Upload multiple files to database using ASP.NET MVC
- In this article we will see how to upload multiple files to database.
- The main trick is to select multiple files and post it to the controller's action method.
- The saving code will be same, but will iterate for each file.
Database:
We have a simple table named UploadedFiles. We have three columns in the table:
- FileId - This is primary key identity column.
- ContentType - This column stores the file type.
- ImageBytes - This column is of type varbinary and stores file bytes.
ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FileUpload.ViewModel
{
public class FileUploadViewModel
{
public IEnumerable<HttpPostedFileBase> File { get; set; }
}
}
We have created a IEnumerable property of type HttpPostedFileBase to support multiple uploads. The HttpPostedFileBase type property contains file stream and file information when posted to the controller action.View:
@model FileUpload.ViewModel.FileUploadViewModel
@{
ViewBag.Title = "FileUpload";
}
<h2>FileUpload</h2>
<div>
@using (Html.BeginForm("UploadMultipleFiles", "FileUpload", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(c => c.File, new { type = "file", multiple = "true" })
<input type="submit" style="margin-left:40px;cursor:pointer;" id="upload" value="Upload"/>
}
</div>
We have referred the above View model in the view. We have used TextBoxFor helper to render file control using model binding. We have used htmlAttribute object to set multiple property of control to true which allows to select multiple files.
We have created a form using BeginForm helper. We have also created a submit button. When form is submitted the files are posted to UploadMultipleFiles action method of FileUpload controller
Controller:
[HttpPost]
public ActionResult UploadMultipleFiles(FileUploadViewModel fileModel)
{
FileUploadService service = new FileUploadService();
foreach (var item in fileModel.File)
{
service.SaveFileDetails(item);
}
return View("FileUpload");
}
We have accepted the object of our View model as a parameter. This object has the posted files. The File property of the FileUploadViewModel class is iterated for multiple files and each file is saved to database using SaveFileDetails method.Service:
public class FileUploadService
{
public void SaveFileDetails(HttpPostedFileBase file)
{
UploadedFiles newFile = new UploadedFiles();
newFile.ContentType = file.ContentType;
newFile.ImageBytes = ConvertToBytes(file);
using (FileUploadEntities dataContext = new FileUploadEntities())
{
dataContext.UploadedFiles.AddObject(newFile);
dataContext.SaveChanges();
}
}
public byte[] ConvertToBytes(HttpPostedFileBase file)
{
byte[] imageBytes = null;
BinaryReader reader = new BinaryReader(file.InputStream);
imageBytes = reader.ReadBytes((int)file.ContentLength);
return imageBytes;
}
}
The SaveFileDetails saves the file data to the database. The ConvertToBytes method converts the stream to file bytes.Conclusion:
- We need to set the multiple attribute to true of the file control.
- The view model's property should of type IEnumerable.
- Iterate on the posted files and save it to the database.
Screenshots:
Hello, I like this tutorial but I don' t know where are UploadedFiles and FileUploadEntities classes. Can you, please, put here the project or complete solution to download and test by us. Thanks
ReplyDeleteGreat
ReplyDeleteOnline MVC Training