This is the author is often asked a question, how to download the file through the ASP.net, this problem can be small, we first from the small start.
This is the author is often asked a question, how to download the file through the ASP.net, this problem can be small, we first from the small start. The easiest way to get the user to download a file is through the Response.Redirect directive:
Response.Redirect ("Test.doc")
You can put the above line of instructions in the button's click event, when the user clicks the button, the page will be transferred to the Word file, resulting in the download effect.
But there are a few questions about this download:
1. Unable to download nonexistent files: For example, if we want the text generated by the dynamic (temporary) of a program to be downloaded as a file (that is, the file is not actually present, it is dynamically generated), it cannot be downloaded.
2. Unable to download files stored in the database: This is a similar issue, the file does not really exist, but is stored in the database in a certain location (a record in a field), can not download.
3. Cannot download a file that does not exist in a Web folder: The file does exist, but the folder is not a shared Web folder, for example, the location of the file is in C:\winnt, you don't want to think of the folder as a Web folder? Because you cannot use Redirect to point to this location, you cannot download it.
4. After downloading the file, the original page will disappear.
The typical situation is that we want the user to download a. txt file, or a. csv format for Excel files, but ...
1. This file may be generated dynamically through the ASP.net program, rather than the file that actually exists on the server side;
2. Or although it exists in the server side of an entity location, but we do not want to expose this location (if this location is open, it is likely that users with no permissions can also enter a URL on the Web site directly to obtain!!!)
3. Or this location is not in the folder where the Web site virtual path is located. (e.g. C:\Windows\System32 ...)
At this point, we have to adopt a different approach:
Shared Function DownloadFile (ByVal webform as System.Web.UI.Page, ByVal filenamewhenuserdownload as String, ByVal Filebod Y 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 (A) & System.Web.HttpUtility.UrlEncode (Filenamewhenuserdownload, System.Text.Encoding.UTF8 ) & Chr (34))
WebForm.Response.ContentType = "Application/octet-stream"
' File contents
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 on the server side of the entity path, you can use the following function:
Shared Sub DownloadFile (ByVal webform as System.Web.UI.Page, ByVal filenamewhenuserdownload as String, ByVal FilePath as S Tring
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 (A) & System.Web.HttpUtility.UrlEncode (Filenamewhenuserdownload, System.Text.Encoding.UTF8 ) & Chr (34))
WebForm.Response.ContentType = "Application/octet-stream"
' File contents
WebForm.Response.Write (System.IO.File.ReadAllBytes (FilePath))
WebForm.Response.End ()
End Sub
These two functions, which are downloaded, should solve the file download problem for most developers in asp.net.