When a new window is opened for download, You need to close it after download.
One is effective for small files:
System. Io. fileinfo file = new system. Io. fileinfo ("f :\\ MP3 \ MP3 \ do not cry my most loved ones ");
Response. Clear ();
Response. clearheaders ();
Response. Buffer = false;
Response. charset = "gb2312 ";
Response. contentencoding = system. Text. encoding. utf8;
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode ("Don't Cry "));
Response. addheader ("Content-Length", file. length. tostring ());
Response. writefile (file. fullname );
Response. Flush ();
Response. End ();
This code can output the file and close the newly opened form.
For large files, the above Code is not good, because the above Code is output only after all files are cached on the server. If your server has a high traffic, the file size is large. We do not recommend that you use the above method.
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 kind of code is 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.