ASP. NET mvc5 Download File method

Source: Internet
Author: User

The controller comes with a Filecontentresult that allows us to easily return files to the server, reducing the number of steps. To download files, such as video, text, images such as browser-supported files, the default will be opened by the browser. When you want to make it a download, you also need to set up a response header to tell the browser how to handle it.

         Public ActionResult DownLoadFile (string path, String fileName, String contentType)        {            Response.AddHeader (" Content-disposition "," attachment; Filename= "+ server.urlencode (fileName));            Return File (Path,contenttype);        }

Above, in JS using location.href = Filedownloadurl Such a simple way can be implemented in the ASP. NET mvc5 download file. The key is this content-disposition, and its content is about how to handle it, and what the default file name is. Attachment is the attachment to the meaning of processing, followed by the file name, URL encoding processing can be avoided because of the file name with a space, and so on.

Aside from this, this is done by setting the response header to ensure that the file is downloaded as an attachment as early as a year ago, when I saw a face, and did not remember the detailed understanding, and now used, vaguely remember as if used once, of course, do not remember how to write, online search, and can not find, Or you have to knock down a yard by one word, so impressive and easy to find later. What reprint ah, copy and paste are floating clouds, who Code who know!

ASP. NET mvc5 Download File method

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.