1

I'm working on ASP.NET MVC-5 and i have a button that lets me select multiple files for input, so it works with this code

<input type="file" name="file" multiple>

How do i fetch the files to use them within my program logic in the [HttpPost] action from the controller?

1
  • Please note that the model-view-controller tag is intended for questions about the pattern. Use asp.net-mvc and/or one of the version-specific tags when asking questions about ASP.NET MVC. Commented Jun 6, 2015 at 6:15

1 Answer 1

1

In this case, you get the uploaded files from HttpPostedFileBase array in controller action.

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase[] uploadedfiles)
        {
            try
            {
                // Loop through array fro getting files
                foreach (HttpPostedFileBase file in files)
                {
                    // get current file name
                    string filename = System.IO.Path.GetFileName(file.FileName);
                    //Saving the file in relative path (server folder)
                    file.SaveAs(Server.MapPath("~/Images/" + filename));
                    string filepathtosave = "Images/" + filename;

                    /* code for saving the image into database */

                }

                ViewBag.Message = "File Uploaded successfully.";
            }
            catch
            {
                ViewBag.Message = "Error while uploading the files.";
            }
            return View();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.