ASP. NET file download function (useful stuff)

Source: Internet
Author: User

Add this in your page_loadCode:

Page. response. Clear ();
 
Bool Success = Responsefile (page. Request, page. response, "Target FileName " , @" Source FilePath " , 1024000 );
 
If ( ! Success)
Response. Write (
" An error occurred while downloading the file! " );
Page. response. End ();

Function Code for file download: 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 ;

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 ());
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 (
Int. Parse (pack. tostring ())));
Thread. Sleep (sleep );
}

Else
{
I
=Maxcount;
}

}

}

Catch
{
Return False;
}

Finally
{
BR. Close ();

Myfile. Close ();
}

}

Catch
{
Return False;
}

Return   True ;
}

in this way, when downloading an object, the open/save window is displayed no matter what format the object is.

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.