ASP. NET directly downloads files from the web page without referencing the File URL.

Source: Internet
Author: User
Tags file url

Function Name: ResponseFile
Function: the client downloads an object from the server.
Return Value: True indicates that the server response is successful. False indicates that the server response fails.
Parameters:
PageResponse responds to the client's Response object and uses Page. Response to reference
DownloadFileName the file name downloaded from the client
LocalFilePath path of the file to be downloaded on the server
The buffer size of the DownloadBuffer server for reading files, measured in KB.
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

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.