[Post] ASP. NET file downloading

Source: Internet
Author: User
Original post http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/default.aspx

Occasionally found this articleArticle, Browsed the keyCodeCopy.

1. download the file instead of opening the file ( Forcing a File Download Dialog ): Setting Content-Disposition Response Header Sub Page_load ( Byval Sender As   Object , Byval E As Eventargs)
Dim Dldir As   String   =   " Downloadfiles/ "
Dim Strfilename As   String   = Request. querystring ( " Filename " )
Dim Path As   String   = Server. mappath (_
Dldir
+ Request. querystring ( " Filename " ))
Dim Todownload As System. Io. fileinfo =   New System. Io. fileinfo (PATH)

If Issafefilename (strfilename) Andalso Todownload. exists Then
Response. Clear ()
Response. addheader (
" Content-Disposition " ,_
" Attachment; filename = "   & Todownload. Name)
Response. addheader (
" Content-Length " ,_
File. length. tostring ())
Response. contenttype
=   " Application/octet-stream "
Response. writefile (file. fullname)
Response. End ()
Else
Bindfiledatatogrid (
" Name " )
End   If
End sub

2. multipart download of large files ( Downloading huge files in small pieces ): Writing File Chunks To the client Sub Page_load ( Byval Sender As   Object , Byval E As Eventargs)
Dim Dldir As   String   =   " Downloadfiles/ "
Dim Strfilename As   String   = Request. querystring ( " Filename " )
Dim Path As   String   = Server. mappath (_
Dldir
+ Request. querystring ( " Filename " ))
Dim Todownload As System. Io. fileinfo =   New System. Io. fileinfo (PATH)

If Issafefilename (strfilename) Andalso Todownload. exists Then
Const Chunksize As   Long   =   10000
Dim Buffer (chunksize) As   Byte

Response. Clear ()
Using istream
As Filestream = File. openread (PATH)
Dim Datalengthtoread As   Long   = Istream. Length
Response. contenttype
=   " Application/octet-stream "
Response. addheader (
" Content-Disposition " ,_
" Attachment; filename = "   & Todownload. Name)
While Datalengthtoread >   0   Andalso Response. isclientconnected
Dim Lengthread As   Integer   = _
Istream. Read (buffer,
0 , Chunksize)
Response. outputstream. Write (buffer,
0 , Lengthread)
Response. Flush ()
Datalengthtoread
= Datalengthtoread - Lengthread
End   While
End Using
Response. Close ()
Else
Bindfiledatatogrid (
" Name " )
End   If
End sub

3. Solve the large memory loss caused by binarywrite or large file downloads: Using Transmitfile Sub Page_load ( Byval Sender As   Object , Byval E As Eventargs)
Dim Dldir As   String   =   " Downloadfiles/ "
Dim Strfilename As   String   = Request. querystring ( " Filename " )
Dim Path As   String   = Server. mappath (_
Dldir
+ Request. querystring ( " Filename " ))
Dim Todownload As System. Io. fileinfo =   New System. Io. fileinfo (PATH)

If Issafefilename (strfilename) Andalso Todownload. exists Then
Response. Clear ()
Select   Case System. Io. Path. getextension (strfilename)
Case   " . Zip "
Response. contenttype
=   " Application/X-zip-compressed "
Response. addheader (
" Content-Disposition " ,_
" Attachment; filename = newdl _ "   + Todownload. Name)
Response. transmitfile (PATH)

Case   Else
'File Extension
Not Supported.
End   Select
Response. End ()
Else
Bindfiledatatogrid (
" Name " )
End   If
End sub

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.