HTTP Posted File Helper V.1.0.2 Nugget Link
Installing..
- PM> Install-Package HttpPostedFileHelper
Usage
Processing Single Files (Basic Usage)
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult UploadFile(HttpPostedFileBase file) {
- //Instanciate the Filehelper class to create a Filehelper Object
- FileHelper filehelper = new FileHelper();
- filehelper.ProcessFile(file, "/MyTargetLocation");
- return view();
- }
Processing Multiple Files
This makes a provision for multiple files being uploaded to the Server with an overridden method for processing an IEnumerable of files (HttpPostedFileBase Collection).
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult UploadFile(Model model, IEnumerable < HttpPostedFileBase > file) {
- FileHelper filehelper = new FileHelper();
- // ProcessFile returns count of files processed
- int postedfiles = filehelper.ProcessFile(file, "/MyTargetLocation");
- if (postedfiles > 0) {
- //files were written successfully
- }
- return View("Home");
- }
Asynchronous File Processing
Processing files can be done asynchronously. This allows large files to be processed in the background thread freeing the main thread.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task < ActionResult > UploadFile(Model model, IEnumerable < HttpPostedFileBase > file) {
- FileHelper filehelper = new FileHelper();
- await filehelper.ProcessFileAsync(file, "/MyTargetLocation");
- //you can do some other work while awaiting
- return View("Home");
- }
Reject File Extensions During Upload
You can specify the file types to be rejected during an upload by supplying a string of the file extensions.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task < ActionResult > UploadFile(Model model, IEnumerable < HttpPostedFileBase > file) {
- FileHelper filehelper = new FileHelper();
- string reject = ".jpeg,.png,.svg";
- int postedfiles = await filehelper.ProcessFileAsync(file, "/MyTargetLocation", reject); //you can do some other work while awaiting
- if (postedfiles > 0) {
- //files were written successfully
- }
- return View("Home");
- }
Thanks for reading this blog. I hope it will help someone.


Join the conversation! Your thoughts help the community grow.