Asp.net is commonly used to control the download of a file. The function supports continuous transmission of multiple lines (modify the control of multiple lines on your own)

Source: Internet
Author: User

The file size can be controlled. Download name: single block data size: single block data interval and MIME type are easy to use.


Public   Shared   Function responsefile () Function Responsefile ( Byval _ Request As Httprequest, Byval _ Response As Httpresponse, Byval _ Filename As   String , Byval _ Fullpath As   String , Byval Blocksize As   Long , Byval Sleep As   Integer , Optional   Byval Mimetype As   String   =   " Application/octet-stream " ) As   Boolean
Try



Dim Myfile As   New Filestream (_ fullpath, filemode. Open, fileaccess. Read, fileshare. readwrite)

Dim BR As   New Binaryreader (myfile)


Try

_ Response. addheader ( " Accept-ranges " , " Bytes " )
_ Response. Buffer =   False
Dim Filelength As   Long   = Myfile. Length
Dim Startbytes As   Long   =   0




If ( Not _ Request. headers ( " Range " ) Is   Nothing ) Then
_ Response. statuscode =   206
Dim Range () As   String   = _ Request. headers ( " Range " ). Split ( " =- " . Tochararray)
Startbytes = Convert. toint64 (range ( 1 ))

End   If

_ Response. addheader ( " Content-Length " , (Filelength - Startbytes). tostring ())
If (Startbytes <>   0 ) Then
_ Response. addheader ( " Content-Range " , String . Format ( " Bytes {0}-{1}/{2} " , Startbytes, filelength -   1 , Filelength ))

End   If
_ Response. addheader ( " Connection " , " Keep-alive " )
_ Response. contenttype = Mimetype
_ Response. addheader ( " Content-Disposition " , " Attachment; filename = "   + Httputility. urlencode (_ filename, system. Text. encoding. utf8 ))

BR. basestream. Seek (startbytes, seekorigin. Begin)
Dim Maxcount As   Integer   =   CINT (Math. Floor (filelength - Startbytes) / Blocksize) +   1 )
For I As   Integer   =   0   To Maxcount -   1

If _ Response. isclientconnected Then
_ Response. binarywrite (Br. readbytes (blocksize ))
Thread. Sleep (sleep)

Else
I = Maxcount
End   If

Next
Catch Ex As Exception

Return   False
End   Try





Catch Ex As Exception

Return   False

End   Try
End Function

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.