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 ([Link])
{
if (file != null)
{
[Link] = 1;
[Link] = [Link];
[Link] = [Link];
[Link] = new byte[[Link]];
[Link]([Link], 0, [Link]);
[Link](tblObj);
[Link]();
[Link]();
}
else
{
}
}
}
catch (Exception ex)
{
}
return RedirectToAction("ViewFiles", "ViewUplodedFile");
}
[Link]
@model [Link]
@{
[Link] = "Upload File";
Layout = "~/Views/Shared/_EmployerMasterLayout.cshtml";
}
<h2>FileUpload</h2>
@* enctype = "multipart/form-data" must mention in BeginForm *@
@using ([Link]("FileUpload", "UserImgResume",[Link], new{enctype = "multipart/form-data" }))
{
@[Link]()
<div class="form-horizontal">
<h4>Upload File</h4>
<hr />
@[Link](true, "", new { @class = "text-danger" })
<div class="form-group">
@[Link]("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 [Link] 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 [Link] where [Link] == [Link] select d).ToList();
byte[] fileBytes = item[0].fileContent;
string fileName = item[0].fileName;
string fileType = item[0].fileType;
return File(fileBytes, fileType, fileName);
}
[Link]
@model IEnumerable<[Link]>
@{
[Link] = "ViewFiles";
Layout = "~/Views/Shared/_EmployerMasterLayout.cshtml";
}
<h2>View Uploaded Files</h2>
<table>
@foreach (var item in Model)
{
if ([Link] == "image/jpeg" || [Link] == "image/jpg" || [Link] == "image/gif" ||
[Link] == "image/png")
{
<tr>
<td style="padding: 15px;">
@{
var base64 = Convert.ToBase64String([Link]);
var imgsrc = [Link]("data:image/gif;base64,{0}", base64);
}
<img src="@imgsrc" style="max-height:100px;max-width:100px" />
</td>
</tr>
}
if ([Link] == "application/[Link]" ||
[Link] == "application/[Link]-excel" || [Link] == "application/pdf")
{
<tr>
<td>
@[Link]([Link], "DownloadResume", "ViewUplodedFile", new { [Link] },
new { @class = "btn btn-info" })
</td>
</tr>
}
}
</table>