ASP. NET close the download window

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.