

Here StringBuilder - sbFiles object is being used to store file names which are being uploaded. The try catch block will ensure exception is caught if it occurs and will send appropriate error message using a ViewBag to our View.

Without this validation our code can run into run-time exceptions. The if condition for checking files = null || files.Count = 0 || files = null will ensure that the files object is not null and it contains files to be uploaded. In this method, we are first checking for files object is valid or not. The Index method accepts a List which we are using to get the file information posted from the View. String filePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + fileName) String fileName = "file-" + Guid.NewGuid() + "-" + ("ddMMyyyyHHmmss") + Path.GetExtension(file.FileName) StringBuilder sbFiles = new StringBuilder() If (files = null || files.Count = 0 || files = null) Public class DefaultController : Controller Create a new controller in your MVC project with name DefaultController.cs and edit it as below:
