Use Html5 and Asp.net MVC to upload multiple files.

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Form action = "/Home/Upload" enctype = "multipart/form-data" id = "form2" method = "post">
<Input type = "file" name = "fileToUpload" id = "fileToUpload2" multiple = "multiple"/>
<Input type = "submit" value = "submit"/>
</Form>

In Asp.net MVC web application, we can achieve this:
Copy codeThe Code is as follows:
@ Using (Html. BeginForm ("Upload", "Home", FormMethod. Post, new {enctype = "multipart/form-data", id = "form2 "}))
{
<Label for = "file"> Upload Image: </label>
<Input type = "file" name = "fileToUpload" id = "fileToUpload2" multiple = "multiple"/>
<Input type = "submit" value = "Upload Image by submit"/>
}

Assume that this is a View under HomeController, which is about to be submitted to the Upload Action. Check the following server code:
Copy codeThe Code is as follows:
[HttpPost]
Public ActionResult Upload (HttpPostedFileBase [] fileToUpload)
{
Foreach (HttpPostedFileBase file in fileToUpload)
{
String path = System. IO. Path. Combine (Server. MapPath ("~ /App_Data "), System. IO. Path. GetFileName (file. FileName ));
File. SaveAs (path );
}

ViewBag. Message = "File (s) uploaded successfully ";
Return RedirectToAction ("Index ");
}

Okay, that's simple. Here we store the received file in the App_Data folder, and then return the Action of the Index. See the following picture. We can select multiple images from the file selector:
 

You can check the HTML5 feature in those browsers. You can also view the W3C official documentation. We can pass the test in FireFox 14.01.

Hope to help your Web development.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.