-
-
Save jrmackie/2483d49a5b1a67086e85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment