Nugget Link for HTTP Posted File Helper V.1.0.2. This lightweight library eliminates boilerplate when sending files to IIS Web Server by offering a helper class called FileHElper.cs with overloaded methods ProcessFile() and ProcessFIleAsync().


Installing: To install the library, enter the following command in the Visual Studio Nuget Package Manager Console.

    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.