Asp. NET to download files directly from the Web

Source: Internet
Author: User

Function Name: Responsefile
Function: Client downloads a file from the server side
Return value: Returns TRUE indicates that the server responded successfully and False indicates failure
Parameters:
Pageresponse respond to the response object of the client, using the Page.Response reference
DownloadFileName the filename of the client download file
Localfilepath the path of the server-side file to be downloaded
Downloadbuffer the buffer size, in kilobytes, of the server-side read file


Public Function Responsefile (ByRef pageresponse as HttpResponse, ByVal downloadfilename as String, ByVal Localfilepath as String, ByVal Downloadbuffer as Long) as Boolean
Dim Reader as System.IO.FileStream
Dim Buffer () as Byte
Dim Filelength as Long
Dim Filebuffer as Long = 1024 * Downloadbuffer
Dim Readcount as Long
Readcount = Filebuffer
ReDim Buffer (ReadCount-1)
Try
Reader = System.IO.File.OpenRead (Localfilepath)
Filelength = Reader.length
Try
Pageresponse.buffer = False
Pageresponse.addheader ("Connection", "keep-alive")
Pageresponse.contenttype = "Application/octet-stream"
Pageresponse.addheader ("Content-disposition", "attachment;filename=" + downloadfilename)
Pageresponse.addheader ("Content-length", filelength.tostring)
While readcount = Filebuffer
Readcount = Reader.read (Buffer, 0, Filebuffer)
ReDim Preserve Buffer (ReadCount-1)
Pageresponse.binarywrite (Buffer)
End While
Response.End ()
Catch ex as Exception
Return False
Finally
Reader.close ()
End Try
Catch ex as Exception
Return False
End Try
Return True
End Function



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.