Normal File Download:
Code
/// <Summary>
/// Output the file to the browser
/// </Summary>
/// <Param name = "_ request"> Page. Request </Param>
/// <Param name = "_ response"> Page. Response </Param>
/// <Param name = "_ filename"> Destination File Name </Param>
/// <Param name = "_ fullpath"> Source File Path </Param>
/// <Param name = "_ speed"> Speed </Param>
/// <Returns> True or false </Returns>
Public Bool Downloadfile (httprequest _ Request, httpresponse _ response, String _ Filename, String _ Fullpath, Long _ Speed)
{
Try
{
Filestream myfile = New Filestream (_ fullpath, filemode. Open, fileaccess. Read, fileshare. readwrite );
Binaryreader br= NewBinaryreader (myfile );
// Binaryreader BR = new binaryreader (myfile );
Try
{
_ Response. addheader ( " Accept-ranges " , " Bytes " );
_ Response. Buffer = False ;
Long Filelength = Myfile. length;
Long Startbytes = 0 ;
Double Pack = 10240 ; // 10 K bytes
// Integer sleep = 200; // 5 times per second, that is, 5*10 K bytes per second
Int Sleep = ( Int ) Math. Floor ( 1000 * Pack / _ Speed) + 1 ;
If (_ Request. headers [ " Range " ] ! = Null )
{
_ Response. statuscode = 206 ;
String [] Range = _ Request. headers [ " Range " ]. Split ( New Char [] { ' = ' , ' - ' });
Startbytes = Convert. toint64 (range [ 1 ]);
}
_ Response. addheader ( " Content-Length " , (Filelength - Startbytes). tostring ());
_ Response. addheader ( " Connection " , " Keep-alive " );
_ Response. contenttype = " Application/octet-stream " ;
_ Response. addheader ( " Content-Disposition " , " Attachment; filename = " + httputility. urlencode (_ filename, system. Text. encoding. utf8);
Br. basestream. seek (startbytes, seekorigin. begin);
int maxcount = ( int ) math. floor (filelength - startbytes) / pack) + 1 ;
For ( Int I = 0 ; I < Maxcount; I ++ )
{
If (_ Response. isclientconnected)
{
_ Response. binarywrite (Br. readbytes ( Int . Parse (pack. tostring ())));
System. Threading. thread. Sleep (sleep );
}
Else
{
I = Maxcount;
}
}
}
Catch
{
Return False ;
}
Finally
{
BR. Close ();
Myfile. Close ();
}
}
Catch
{
Return False ;
}
Return True ;
}