How to download an object as an attachment

Source: Internet
Author: User
1 // Output hard disk files, which support large files, resumable transfers, speed limits, and low resource usage
2 // Input parameter _ Request: page. Request object, _ response: page. Response object, _ filename: Download file name, _ fullpath: Download path with file name, _ speed the number of bytes allowed to be downloaded per second
3 // Success or not
4 Public   Static   Bool Responsefile (httprequest _ Request, httpresponse _ response, String _ Filename, String _ Fullpath, Long _ Speed)
5 {
6 Try
7 {
8 Filestream myfile =   New Filestream (_ fullpath, filemode. Open, fileaccess. Read, fileshare. readwrite );
9 Binaryreader br =   New Binaryreader (myfile );
10 Try
11 {
12 _ Response. addheader ( " Accept-ranges " , " Bytes " );
13 _ Response. Buffer =   False ;
14 Long Filelength = Myfile. length;
15 Long Startbytes =   0 ;
16
17 Int Pack =   102400 ; // 10 K bytes
18 // Integer sleep = 200; // 5 times per second, that is, 5*10 K bytes per second
19 Int Sleep = ( Int ) Math. Floor ( 1000   * Pack / _ Speed) +   1 ;
20 If (_ Request. headers [ " Range " ] ! =   Null )
21 {
22 _ Response. statuscode =   206 ;
23 String [] Range = _ Request. headers [ " Range " ]. Split ( New   Char [] {'=','-'} );
24 Startbytes = Convert. toint64 (range [ 1 ]);
25 }
26 _ Response. addheader ( " Content-Length " , (Filelength - Startbytes). tostring ());
27 If (Startbytes ! =   0 )
28 {
29 _ Response. addheader ( " Content-Range " , String . Format ( " Bytes {0}-{1}/{2} " , Startbytes, filelength - 1 , Filelength ));
30 }
31 _ Response. addheader ( " Connection " , " Keep-alive " );
32 _ Response. contenttype =   " Application/octet-stream " ;
33 _ Response. addheader ( " Content-Disposition " , " Attachment; filename = "   + Httputility. urlencode (_ filename, system. Text. encoding. utf8 ));
34  
35 BR. basestream. Seek (startbytes, seekorigin. Begin );
36 Int Maxcount = ( Int ) Math. Floor (filelength - Startbytes) / Pack) +   1 ;
37
38 For ( Int I =   0 ; I < Maxcount; I ++ )
39 {
40 If (_ Response. isclientconnected)
41 {
42_ Response. binarywrite (Br. readbytes (pack ));
43Thread. Sleep (sleep );
44}
45 Else
46 {
47I=Maxcount;
48}
49 }
50 }
51 Catch
52 {
53Return False;
54}
55 Finally
56 {
57BR. Close ();
58Myfile. Close ();
59}
60 }
61 Catch
62 {
63Return False;
64}
65 Return   True ;
66 }

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.