Class File in Edmx File: - ( (,) NOT Null, NOT Null, Null, Null, Null)
Class File in Edmx File: - ( (,) NOT Null, NOT Null, Null, Null, Null)
Class File in Edmx File: - ( (,) NOT Null, NOT Null, Null, Null, Null)
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)
[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 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";
}
<table>