Using asp.net mvc to process file upload and download

Source: Internet
Author: User
Tags file upload

If you only have the ASP.net Web forms background instead of learning asp.net mvc, I think your first experience may be that the service-side controls that have made your programming enjoyable are driving the crane West. FileUpload is one of them, and the absence of this control brings us some minor problems. This article mainly says how to upload files in asp.net mvc, and then how to download the uploaded files from the server.

In Web Forms, when you drag a FileUpload control to the designer, you may not notice that adding an extra attribute to the form tag in the generated HTML enctype= "Multipart/form-data". The FileUpload control itself becomes <input type= "file"/>, and in MVC view there are many ways to do the same, the first HTML is as follows:

<form action="/" method="post" enctype="multipart/form-data">
  <input type="file" name="FileUpload1" /><br />
  <input type="submit" name="Submit" id="Submit" value="Upload" />
</form>

Note that the form label already includes the enctype tag, and the method property is set to "post" so that it does not have to be set more than the default commit by HTTP GET. In this way, using the Html.BeginForm () extension method generates the same HTML as the above:

<%
  using (Html.BeginForm("", "home", FormMethod.Post, new {enctype="multipart/form-data"}))
   {%>
    <input type="file" name="FileUpload1" /><br />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
<% }%>

Note the Name property of the <input type= "file" > tag, which we'll discuss later, as shown in the following code:

OK, now we can browse the local file and then submit the file to the server via the Upload submit button, and the next step is to process the uploaded file on the server side, and when using the FileUpload control, You can easily see if the file is uploaded through the FileUpload HasFile method. But in asp.net mvc it seems not so convenient, you will be closer to the original HTTP, however, an extension method can handle these:

public static bool HasFile(this HttpPostedFileBase file)
{
  return (file != null && file.ContentLength > 0) ? true : false;
}

When you see the corresponding controller class code, you will find that the request object exists as an attribute of the httprequestbase type. Httpreuqestbase is actually a package of HTTP requests, which leaks a lot of attributes, including the Files collection (actually a collection of httpfilecollectionbase), and every element in the collection is HttpPostedFileBase collection, an extension method that is used to ensure that the uploaded file exists. In fact, this works in accordance with the Fileupload.hasfile () method.

It's really easy to use in controller action:

public class HomeController : Controller
{
  public ActionResult Index()
  {
   foreach (string upload in Request.Files)
   {
    if (!Request.Files[upload].HasFile()) continue;
    string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
    string filename = Path.GetFileName(Request.Files[upload].FileName);
    Request.Files[upload].SaveAs(Path.Combine(path, filename));
   }
   return View();
  }
}

Related Article

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.