About Me

My photo
Web application developer with over 2+yrs exp.Has worked on Asp.net mvc,C#,jquery,core java.

Wednesday, November 2, 2011

Multiple file uploads asp.net mvc

Hi Frens,
A quick post of how to do multiple file uploads in asp.net mvc 3
UI:


@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="files" id="file1"/>
        <input type="file" name="files" id="file2"/>
        <input type="file" name="files" id="file3"/>
        <input type="file" name="files" id="file4"/>
        <input type="file" name="files" id="file5"/>
        <input type="submit" value="Upload Files" />
    }



Controller Actions:
[HttpPost]
public ActionResult FileUpload(IEnumerable<HttpPostedFileBase> files)
{
//directory for uploading files
string UploadDirectory = AppDomain.CurrentDomain.BaseDirectory + "Files" + "\\";
 //check for the existence of the directory
  if (!Directory.Exists(UploadDirectory))
            {
                try
                {
                    //create directory if not exists
                    Directory.CreateDirectory(UploadDirectory);
                }
                catch (Exception ex)
                {
                    //return error message if not able to create directory
                    return Content("Oops not able to create directory for saving the file");
                }
            }
            List lstString = new List();
            bool allFileUploaded = true;
            //enumerate through each file in the collection
            foreach (HttpPostedFileBase file in files)
            {
                //check if file was not selected from the file input box
                if (file != null)
                {
                    //check the size of the file uploaded in bytes
                    if (file.ContentLength > 0)
                    {
                        //for ie
                        string fname = Path.GetFileName(file.FileName);
                        string path = Path.Combine(UploadDirectory + fname);
                        try
                        {
                            file.SaveAs(path);
                        }
                        catch (Exception ex)
                        {
                            //store the names of the files which was not uploaded
                            lstString.Add("Failed to upload the file with name "  + fname);
                            allFileUploaded = false;
                        }
                    }
                }
            }
            string fileNames = "";
            if (allFileUploaded == true)
            {
                //success message
                return Content("All file uploaded successfully");
            }
            else
            {
                foreach (string file in lstString)
                {
                    fileNames += file + ",";
                }
                fileNames = fileNames.Substring(0, fileNames.LastIndexOf(",") - 1);
                //show the files which was not uploaded successfully
                return Content("Failed to upload the following files " + fileNames);
            }
        }
       Thanks,
       Sagi