Download the implementation code for the file in ASP.NET

  • 2020-05-16 06:47:22
  • OfStack

This is a question I am often asked, how to download files through ASP.NET, this problem can be big or small, let's start small. When we want the user to download a file, the easiest way is through the Response.Redirect directive:

Response. Redirect (" test doc ")

You can place the above command in the Click event of Button. When the user clicks the button, the page will be redirected to the word file, resulting in the effect of downloading.

But there are several problems with such downloads:

1. You can't download a file that doesn't exist: for example, if you want to download a dynamically (temporarily) generated text from a program as if it were a file (that is, a file that didn't really exist but was generated dynamically), you can't download it.
2. Unable to download files stored in the database: this is a similar problem, the file does not really exist, but it cannot be downloaded when stored in the database somewhere (in a certain field of a certain record).
3. Cannot download files that do not exist in the Web folder: files do exist, but this folder is not an Web folder that can be Shared. For example, the location of the file is C:/winnt. At this point, you cannot download because you cannot point to the location using Redirect.
4. After downloading the file, the original page will disappear.

Typically, we're going to ask the user to download a.txt file, or an Excel file in the.csv format, but...

1. This file may be generated dynamically through the ASP.NET program, rather than actually existing in the Server terminal;
2. Or that it exists in a physical location on the server side, but we don't want to expose the location (if the location is public, users who probably don't have permission can type URL in the url bar to get it directly!!).
3. Or the location is not in the folder where the virtual path of the site is located. (such as C: / Windows System32...).

In this case, we have to do it in a different way:
 
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") 
  ' The 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" 
  ' The file content  
  WebForm.Response.Write(FileBody) 
  WebForm.Response.End() 
End Function 

The above code is to download a dynamically generated text file. If the file already exists in the entity path on the server side, the following functions can be used:
 
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") 
  ' The 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" 
  ' The file content  
  WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath)) 
  WebForm.Response.End() 
End Sub 

The above two download file functions should solve the file download problem for most developers in ASP.NET.

Related articles: