Asp.net displays the download webpage program with the download prompt

Source: Internet
Author: User

You must develop the file download function for website creation. The following three methods can be used to download files:
1. download code using asp
<%
Filename = Request. QueryString ("FileName ")
If filename = "" then
Response. Write "Enter the filename parameter to specify the downloaded file name"
Else
Response. ContentType = "application/octet-stream"
Response. AddHeader "content-disposition", "attachment; filename =" & filename
Set FileStream = Server. CreateObject ("Adodb. Stream ")
FileStream. Mode = 3
FileStream. Type = 1
FileStream. Open
FileStream. LoadFromFile (Server. MapPath (filename ))

Response. BinaryWrite (FileStream. Read)

FileStream. Close ()
Set FileStream = nothing
End if
%> Save the above Code as an asp-type file. The usage is similar to: download. asp? Filename=a.gif.
2. Use WebClient
Add the following code to the Download button event:
System. Net. WebClient wc = new System. Net. WebClient ();
Wc. DownloadFile ("http: // localhost/a.gif", "c: a.gif ");
The code above downloads the C drive of the client without any prompts from the.gif file on the server side. It is terrible if there are no prompts, but sometimes it is necessary to do so. This code can also run on the desktop.

Asp.net displays the download webpage program with the download prompt
// Open the object to be downloaded
System. IO. FileStream r = new System. IO. FileStream (FileName, System. IO. FileMode. Open );
// Set basic information
Response. Buffer = false;
Response. AddHeader ("Connection", "Keep-Alive ");
Response. ContentType = "application/octet-stream ";
Response. AddHeader ("Content-Disposition", "attachment; filename =" + System. IO. Path. GetFileName (FileName ));
Response. AddHeader ("Content-Length", r. Length. ToString ());

While (true)
{
// Open the buffer space
Byte [] buffer = new byte [1024];
// Read the file data
Int leng = r. Read (buffer, 0, 1024 );
If (leng = 0) // end at the end of the file
Break;
If (leng = 1024) // read the file data length is equal to the buffer length, directly write the buffer data
Response. BinaryWrite (buffer );
Else
{
// Read the file data is smaller than the buffer, and the buffer size is redefined. It is only used to read the last data block of the file.
Byte [] B = new byte [leng];
For (int I = 0; I <leng; I ++)
B [I] = buffer [I];
Response. BinaryWrite (B );
}
}
R. Close (); // Close the downloaded file
Response. End (); // End object download
This method has a download prompt box. The server can know when the download is complete.

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.