Skip to content

Instantly share code, notes, and snippets.

@itorian
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save itorian/9385989 to your computer and use it in GitHub Desktop.
Save itorian/9385989 to your computer and use it in GitHub Desktop.
Upload File (Single file at once) to SQL Server Database from Index.cshtml view page and download them from Download.cshtml view page. If you want to upload multiple files once read this gist https://gist.github.com/itorian/9389738
@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();
byte[] uploadFile = new byte[model.File.InputStream.Length];
model.File.InputStream.Read(uploadFile, 0, uploadFile.Length);
fileUploadModel.FileName = model.File.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);
}
}
}
@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" })
@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 HttpPostedFileBase File { get; set; }
}
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" />
@solimansoliman
Copy link

goood

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