I have tested the following to better solve the problem:
Public static bool responsefile (httprequest _ Request, httpresponse _ response, string _ filename, string _ fullpath, long _ speed)
{
Try
{
Filestream myfile = new filestream (_ fullpath, filemode. Open, fileaccess. Read, fileshare. readwrite );
Binaryreader BR = new binaryreader (myfile );
Try
{
_ Response. addheader ("Accept-ranges", "bytes ");
_ Response. Buffer = false;
Long filelength = myfile. length;
Long startbytes = 0;
Int pack = 10240; // 10 K bytes
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 ());
If (startbytes! = 0)
{
_ Response. addheader ("content-range", String. Format ("bytes {0}-{1}/{2}", startbytes, fileLength-1, filelength ));
}
_ 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 (pack ));
Thread. Sleep (sleep );
}
Else
{
I = maxcount;
}
}
_ Response. Flush ();
}
Catch
{
Return false;
}
Finally
{
BR. Close ();
Myfile. Close ();
}
}
Catch
{
Return false;
}
Return true;
}
This type Code Also online by signature
It can also output the file and close the newly opened form.
In addition, the test showed that the server and the customer's operating system were different.
There will also be some unexpected events
In general, if the above Code is not properly closed, you can check the browser settings in advanced.
Furthermore, you can modify response. contenttype = "application/octet-stream" for different output content types. Because of the different contenttypes, the interpretation on the customer end may be different.