You are on page 1of 3

Class file in edmx file

public partial class tblOpsImgResume


{
public int fileID { get; set; }
public int userID { get; set; }
public string fileName { get; set; }
public string fileType { get; set; }
public byte[] fileContent { get; set; }
}

Sql Query
CREATE TABLE [dbo].[tblOpsImgResume](
[fileID] [int] IDENTITY(1,1) NOT NULL Primary key,
[userID] [int] NOT NULL,
[fileName] [varchar](500) NULL,
[fileType] [varchar](500) NULL,
[fileContent] [varbinary](max) NULL)

File Upload Controller


//Creating View for Upload file
public ActionResult FileUpload()
{
return View("FileUpload");
}

[HttpPost]
public ActionResult FileUpload(tblOpsImgResume tblObj, HttpPostedFileBase file)
{

try
{
if (ModelState.IsValid)
{
if (file != null)
{
tblObj.userID = 1;
tblObj.fileName = file.FileName;
tblObj.fileType = file.ContentType;
tblObj.fileContent = new byte[file.ContentLength];
file.InputStream.Read(tblObj.fileContent, 0, file.ContentLength);
DbEntity.tblOpsImgResumes.Add(tblObj);
DbEntity.SaveChanges();
ModelState.Clear();
}
else
{
}
}
}
catch (Exception ex)
{
}
return RedirectToAction("ViewFiles", "ViewUplodedFile");
}
Fileupload.cshtml
@model TRIC.OPS.DataObjectLayer.tblOpsImgResume

@{
ViewBag.Title = "Upload File";
Layout = "~/Views/Shared/_EmployerMasterLayout.cshtml";
}
<h2>FileUpload</h2>
@* enctype = "multipart/form-data" must mention in BeginForm *@
@using (Html.BeginForm("FileUpload", "UserImgResume",FormMethod.Post, new{enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Upload File</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">
@Html.Label("Upload File", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="file" id="file" class="form-control" />
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Upload" class="btn btn-default" />
</div>
</div>
</div>
}

ViewUplodedFileController

//Getting tblOpsImgResume data from database


public ActionResult ViewFiles()
{
var item = (from d in DbEntity.tblOpsImgResumes select d ).ToList();
return View(item);
}

//Getting File data in bytes, Using File method returning the file
public ActionResult DownloadResume(tblOpsImgResume obj)
{
var item = (from d in DbEntity.tblOpsImgResumes where d.fileID == obj.fileID select d).ToList();
byte[] fileBytes = item[0].fileContent;
string fileName = item[0].fileName;
string fileType = item[0].fileType;
return File(fileBytes, fileType, fileName);
}
ViewFiles.cshtml
@model IEnumerable<TRIC.OPS.DataObjectLayer.tblOpsImgResume>

@{
ViewBag.Title = "ViewFiles";
Layout = "~/Views/Shared/_EmployerMasterLayout.cshtml";
}

<h2>View Uploaded Files</h2>

<table>

@foreach (var item in Model)


{
if (item.fileType == "image/jpeg" || item.fileType == "image/jpg" || item.fileType == "image/gif" ||
item.fileType == "image/png")
{
<tr>
<td style="padding: 15px;">
@{
var base64 = Convert.ToBase64String(item.fileContent);
var imgsrc = string.Format("data:image/gif;base64,{0}", base64);
}
<img src="@imgsrc" style="max-height:100px;max-width:100px" />
</td>
</tr>
}
if (item.fileType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
item.fileType == "application/vnd.ms-excel" || item.fileType == "application/pdf")
{
<tr>
<td>
@Html.ActionLink(item.fileName, "DownloadResume", "ViewUplodedFile", new { item.fileID },
new { @class = "btn btn-info" })
</td>
</tr>
}
}
</table>