<div id="UploadSection">
<% using (Html.BeginForm("fileUpload", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%><br />
<p><input type="file" name="fileUpload1" /> </p>
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
public ActionResult FileUpload(HttpPostedFileBase Files)
{
DBContext dataContext = new DBContext();
foreach (string upload in Request.Files)
{
byte[] fileData = new byte[Request.Files[upload].InputStream.Length];
Request.Files[upload].InputStream.Read(fileData, 0, Convert.ToInt32(Request.Files[upload].InputStream.Length));
System.Data.Linq.Binary binaryFile = new System.Data.Linq.Binary(fileData);
FileDump record = new FileDump { FileData = binaryFile, FileName = System.IO.Path.GetFileName(Request.Files[upload].FileName) };
dataContext.FileDumps.InsertOnSubmit(record);
dataContext.SubmitChanges();
}
var returnData = dataContext.FileDumps;
ViewData.Model = returnData.ToList();
return View();
}
public FileContentResult FileDownload(int id)
{
byte[] fileData;
string fileName;
DBContext dataContext = new DBContext();
var record = from p in dataContext.FileDumps
where p.ID == id
select p;
fileData = (byte[])record.First().FileData.ToArray();
fileName = record.First().FileName;
return File(fileData, "text", fileName);
}
Ripped from here[^]
-KR