Transferred from
Http://5543541.qzone.qq.com/blog/23 System. Io. Stream istream = Null ;
Byte [] Buffer = New Byte [ 10240 ];
Int Length;
Long Datatoread;
String Filepath = " File storage address " ;
String Filename = System. Io. Path. getfilename (filepath );
Istream = New System. Io. filestream (filepath, system. Io. filemode. Open, system. Io. fileaccess. Read, system. Io. fileshare. Read );
Response. Clear ();
Datatoread = Istream. length;
Long P = 0 ;
If (Request. headers [ " Range " ] ! = Null )
{
Response. statuscode = 206 ;
P = Long . Parse (request. headers [ " Range " ]. Replace ( " Bytes = " , "" ). Replace ( " - " , "" ));
}
If (P ! = 0 )
{
Response. addheader ( " Content-Range " , " Bytes " + P. tostring () + " - " + (( Long ) (Datatoread - 1 ). Tostring () + " / " + Datatoread. tostring ());
}
Response. addheader ( " Content-Length " ,(( Long ) (Datatoread - P). tostring ());
Response. contenttype = " Application/octet-stream " ;
Response. addheader ( " Content-Disposition " , " Attachment; filename = " + System. Web. httputility. urlencode ( " Real file name " ));
Istream. Position = P;
Datatoread = Datatoread - P;
While (Datatoread > 0 )
{
If (Response. isclientconnected)
{
Length = Istream. Read (buffer, 0 , 10240 );
Response. outputstream. Write (buffer, 0 , Length );
Response. Flush ();
Buffer = New Byte [ 10240 ];
Datatoread = Datatoread - Length;
}
Else
{
Datatoread = - 1 ;
}
}
Istream. Close ();
Response. End ();