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