0% found this document useful (0 votes)
54 views

Class File in Edmx File: - ( (,) NOT Null, NOT Null, Null, Null, Null)

The document describes a file upload system using ASP.NET MVC including class definitions for storing file data in a SQL database table, controllers for uploading files and viewing/downloading uploaded files, and views for the upload and file listing pages. A file upload form submits file data to a controller which saves it to the database and redirects. Another controller retrieves file data from the database and returns files for download or displays images inline.

Uploaded by

Koundinya Sarma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Class File in Edmx File: - ( (,) NOT Null, NOT Null, Null, Null, Null)

The document describes a file upload system using ASP.NET MVC including class definitions for storing file data in a SQL database table, controllers for uploading files and viewing/downloading uploaded files, and views for the upload and file listing pages. A file upload form submits file data to a controller which saves it to the database and redirects. Another controller retrieves file data from the database and returns files for download or displays images inline.

Uploaded by

Koundinya Sarma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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>

You might also like