Public Void Download ( String Filename, page R)
{
If (Filename ! = "" )
{
String Path = R. server. mappath (filename );
System. Io. fileinfo File = New System. Io. fileinfo (PATH );
If (File. exists)
{
R. response. Clear ();
R. response. addheader ( " Content-Disposition " , " Attachment; filename = " + File. Name );
R. response. addheader ( " Content-Length " , File. length. tostring ());
R. response. contenttype = " Application/octet-stream " ;
R. response. Filter. Close ();
R. response. writefile (file. fullname );
R. response. End ();
}
Else
{
R. response. Write ("This file does not exist.");
}
}
Example 2:
Private Void Newdownload ( String Path, String Filename)
{ // PATH is the absolute file path, and filename is the file path
Filestream FS = New Filestream (path, filemode. Open, fileaccess. Read );
Binaryreader R = New Binaryreader (FS );
Response. addheader ( " Content-Disposition " , " Attachment; filename = " + Filename );
Response. charset = " Gb2312 " ;
Response. contenttype = " Application/octet-stream " ;
Response. binarywrite (R. readbytes (convert. toint32 (FS. Length )));
Response. Flush ();
FS. Close ();
Try
{
File. Delete (PATH );
}
Catch (System. Exception ee)
{
StringFF=Ee. tostring ();
}
}
Example 3: File Download # Region File Download
Private Void Downloadfile ( String Address, String Filename)
{ // Address: The local path where the filename file is stored.
WebClient Client = New WebClient ();
Client. downloadfile (address, filename );
Stream Str = Client. openread (Address );
Streamreader = New Streamreader (STR );
Byte [] Mbyte = New Byte [Str. Length + 1 ];
Int Allmybyte = ( Int ) Mbyte. length;
Int Startmbyte = 0 ;
While (Allmybyte > 0 )
{
Int M = Str. Read (Mbyte, startmbyte, allmybyte );
If (M = 0 )
{
Break;
}
Startmbyte + = M;
Allmybyte -= M;
}
Filestream fstr = New Filestream (filename, filemode. openorcreate, fileaccess. Write );
Fstr. Write (Mbyte, 0 , Startmbyte );
Str. Close ();
Fstr. Close ();
}
# Endregion