Asp.net File Download

Source: Internet
Author: User

Multi-thread resumable upload

 

1 Public   Bool Responsedownload ( String Path ){
2 Long Filelength;
3 Long Startrange =   0 ;
4 Int Pack =   10240 ; // 10 K
5 String Filename = Path. getfilename (PATH );
6 Try {
7 Filestream dstream =   New Filestream (path, filemode. Open, fileaccess. Read );
8 Byte [] Buffer =   New   Byte [Pack];
9 // Binaryreader BR = new binaryreader (dstream );
10 Try {
11 Page. response. Clear ();
12 Response. Buffer =   False ;
13 Response. addheader ( " Accept-ranges " , " Bytes " );
14
15 Filelength = Dstream. length;
16 Long SP =   0 ;
17 Long EP =   0 ;
18 If (Request. headers [ " Range " ] ! =   Null ){
19 Response. statuscode =   206 ;
20 String [] Range = Request. headers [ " Range " ]. Split ( New   Char [] { ' = ' , ' - ' });
21 If (Range. Length =   3 &&! String . Isnullorempty (range [ 2 ]) {
22 EP = Convert. toint64 (range [ 2 ]);
23 }
24 SP = Convert. toint64 (range [ 1 ]);
25 If (Ep < SP ){
26 EP =   0 ;
27 }
28 }
29 Long Contlength = EP =   0   ? Filelength - SP: EP - SP +   1 ;
30
31 If (SP ! =   0 ){
32 Response. addheader ( " Content-Range " , String. Format ( " Bytes {0}-{1}/{2} " , SP, EP =   0   ? Filelength -   1 : EP, filelength ));
33 }
34
35 Response. addheader ( " Content-Length " , Contlength. tostring ());
36 Response. addheader ( " Content-Disposition " , " Attachment; filename = "   + System. Web. httputility. urlencode (filename, system. Text. encoding. utf8 ));
37 Response. addheader ( " Connection " , " Keep-alive " );
38 Response. contenttype =   " Application/octest-stream " ;
39 Response. Flush ();
40
41 // Calculate file size;
42 Long Maxcount = EP = 0 ? Filelength: EP + 1 ;
43
44 Maxcount = Maxcount - SP;
45
46 // Move file handler to range point.
47 Dstream. Seek (SP, seekorigin. Begin );
48
49 While (Maxcount >   0 && Response. isclientconnected ){
50 Int Lengthread = Dstream. Read (buffer, 0 , Convert. toint32 (pack ));
51 Response. outputstream. Write (buffer, 0 , Lengthread );
52 Response. Flush ();
53 Maxcount -= Pack;
54 }
55
56 }
57 Catch (Exception e ){
58 Response. Write ( " <H3> error: "   + E. Message );
59 Return   False ;
60 }
61 Finally {
62 Dstream. Close ();
63 }
64
65 }
66 Catch (Exception ex ){
67 Response. Write ( " <H3> error: "   + Ex. Message );
68 Return   False ;
69 }
70 Return   True ;
71 }

 

 

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.