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.