Skip to content

Instantly share code, notes, and snippets.

@itorian
Last active August 29, 2015 13:57
Show Gist options
  • Save itorian/9389738 to your computer and use it in GitHub Desktop.
Save itorian/9389738 to your computer and use it in GitHub Desktop.
Upload Files (multiple files once) to SQL Server Database from Index.cshtml view page and download them from Download.cshtml view page. In the previous gist we saw code to upload single file at once here https://gist.github.com/itorian/9385989
@model IEnumerable<MvcFileUploadToDB.Models.FileUploadDBModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Upload New", "Index")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FileName)
</th>
<th>
@Html.DisplayNameFor(model => model.File)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FileName)
</td>
<td>
@Html.ActionLink("Download", "Filedownload", new { id=item.Id})
</td>
</tr>
}
</table>
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MvcFileUploadToDB.Models
{
public class FileUploadDBContext : DbContext
{
public FileUploadDBContext()
: base("name=FileUploadDBContext")
{
}
public System.Data.Entity.DbSet<MvcFileUploadToDB.Models.FileUploadDBModel> FileUploadDBModels { get; set; }
}
}
using MvcFileUploadToDB.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace MvcFileUploadToDB.Controllers
{
public class HomeController : Controller
{
private FileUploadDBContext db = new FileUploadDBContext();
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
FileUploadDBModel fileUploadModel = new FileUploadDBModel();
foreach (var item in model.File) //3rd change
{
byte[] uploadFile = new byte[item.InputStream.Length];
item.InputStream.Read(uploadFile, 0, uploadFile.Length);
fileUploadModel.FileName = item.FileName;
fileUploadModel.File = uploadFile;
db.FileUploadDBModels.Add(fileUploadModel);
db.SaveChanges();
}
return Content("File Uploaded.");
}
public ActionResult Download()
{
return View(db.FileUploadDBModels.ToList());
}
public FileContentResult FileDownload(int? id)
{
byte[] fileData;
string fileName;
FileUploadDBModel fileRecord = db.FileUploadDBModels.Find(id);
fileData = (byte[])fileRecord.File.ToArray();
fileName = fileRecord.FileName;
return File(fileData, "text", fileName);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
@model MvcFileUploadToDB.Models.MyViewModel
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(x => x.File)
@Html.TextBoxFor(x => x.File, new { type = "file", multiple = "true" }) @*1st change*@
@Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">Upload File</button>
}
</div>
<p>
@Html.ActionLink("Download File", "Download")
</p>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MvcFileUploadToDB.Models
{
public class MyViewModel
{
[Required]
[DisplayName("Select File to Upload")]
public IEnumerable<HttpPostedFileBase> File { get; set; } //2nd change
}
public class FileUploadDBModel
{
public int Id { get; set; }
public string FileName { get; set; }
public byte[] File { get; set; }
}
}
<add name="FileUploadDBContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=DelContext-20140305142934; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|DelContext-20140305142934.mdf"
providerName="System.Data.SqlClient" />
@jrmackie
Copy link

jrmackie commented Feb 2, 2015

Hi, Great article. I have been looking for just such an sample thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment