ASP. NET directly downloads files from the web page without referencing the File URL.
Source: Internet
Author: User
'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.
'Parameter:
'Pageresponse responds to the client's response object, which is referenced by page. Response
'Downloadfilename the file name downloaded from the client
'Localfilepath path of the file to be downloaded on the server
'Downloadbuffer buffer size of the file read by the server, 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
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