Explanation: how to download files in ASP. NET

Source: Internet
Author: User
Explanation: how to download files in ASP. NET
Source: blog Author: dotnetWalker

This is a frequently asked question. How to download files through ASP. NET is a big but small problem. We should start from an early age. When we want users to download an object, the simplest way is through the Response. Redirect command:

 
This is a frequently asked question. How to download files through ASP. NET is a big but small problem. We should start from an early age. When we want users to download an object, the simplest way is through the Response. Redirect command:

The following is a reference clip:
Response. Redirect ("test.doc ")

You can place the preceding command line in the Click Event of the Button. When you Click the Button, the webpage will be directed to the word file, resulting in download effect.

However, there are several problems with this download:

1. files that do not exist cannot be downloaded. For example, if we want to download the text generated by the program dynamically (temporarily) as a file (that is, the file does not actually exist in the past, but dynamically generated.

2. unable to download files stored in the database: This is a similar problem. This file does not exist, but is stored in a certain location in the database (a column in a record, it cannot be downloaded.

3. files that do not exist in the Web Folder cannot be downloaded: The file exists, but the folder is not a Web folder that can be shared. For example, the file is located in C: \ winnt, you never want to treat this folder as a Web folder? At this time, you cannot use Redirect to point to this location, so you cannot download.

4. After the file is downloaded, the original page will disappear.

The following figure shows how to download a. txt file or an Excel file in. CSV format,...

1. This file may be dynamically generated through ASP. NET programs, rather than files that do exist on the Server;

2. or although it exists in a physical location on the server side, we do not want to expose this location (if this location is public, users who do not have the permission can also enter a URL in the URL bar to directly obtain it !!!)

3. Or this location is not in the folder where the website virtual path is located. (For example, C: \ Windows \ system32 ...)

At this time, we have to adopt different methods:

The following is a reference clip:
Shared Function DownloadFile (ByVal WebForm As System. Web. UI. Page, ByVal FileNameWhenUserDownload As String, ByVal FileBody As String)
WebForm. Response. ClearHeaders ()
WebForm. Response. Clear ()
WebForm. Response. Expires = 0
WebForm. Response. Buffer = True
WebForm. Response. AddHeader ("Accept-Language", "zh-tw ")
'File name
WebForm. response. addHeader ("content-disposition", "attachment; filename =" & Chr (34) & System. web. httpUtility. urlEncode (FileNameWhenUserDownload, System. text. encoding. UTF8) & Chr (34 ))
WebForm. Response. ContentType = "Application/octet-stream"
'File content
WebForm. Response. Write (FileBody)
WebForm. Response. End ()
End Function

The above code downloads a dynamically generated text file. If the file already exists in the object path on the server, you can use the following function:

The following is a reference clip:
Shared Sub DownloadFile (ByVal WebForm As System. Web. UI. Page, ByVal FileNameWhenUserDownload As String, ByVal FilePath As String)
WebForm. Response. ClearHeaders ()
WebForm. Response. Clear ()
WebForm. Response. Expires = 0
WebForm. Response. Buffer = True
WebForm. Response. AddHeader ("Accept-Language", "zh-tw ")
'File name
WebForm. response. addHeader ("content-disposition", "attachment; filename =" & Chr (34) & System. web. httpUtility. urlEncode (FileNameWhenUserDownload, System. text. encoding. UTF8) & Chr (34 ))
WebForm. Response. ContentType = "Application/octet-stream"
'File content
WebForm. Response. Write (System. IO. File. ReadAllBytes (FilePath ))
WebForm. Response. End ()
End Sub

The above two file downloading functions should solve the file downloading problem of most developers in ASP. NET.

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.