ASP custom file download method.

Source: Internet
Author: User
You can use stream download (memory consumption, less use) or directly go to this file.


<%

Const use_stream = 0' 0. Do not use stream (ADODB. Stream) download 1. Use stream download
Const allow_file_ext = "rar, zip, CHM, Doc, xls, SwF, MP3, GIF, JPG, JPEG, PNG, BMP" 'indicates the extension of the file to be downloaded to prevent source code from being downloaded.

Dim sdownfilepath 'Download file path
Sdownfilepath = trim (Request ("filepath "))
'Or obtain the file path from the database based on the uploaded file ID.

'If sdownfilepath is an absolute path, you must convert sdownfilepath to the relative path of the current file.

'Sdownfilepath = "focus.swf"

Call downloadfile (sdownfilepath)

Function downloadfile (s_downfilepath)
'Determine whether the file name has been passed
If isnull (s_downfilepath) = true or trim (s_downfilepath) = "" then
Outputerr "error: Check the file to be downloaded first. Download failed"
End if

'Determine whether the extension is valid
Dim s_fileext
S_fileext = mid (s_downfilepath, limit Rev (s_downfilepath, ".") + 1)
If instr ("," & allow_file_ext & "," & s_fileext & ",") <= 0 then
Outputerr "error: the file type (" & s_fileext & ") cannot be downloaded. Download failed"
End if

S_downfilepath = Replace (s_downfilepath ,"\","/")

'For the sake of security, some directories are not allowed to download files, which can be processed here
'

'Check whether the server supports FSO
Dim o_fso
On Error resume next
Set o_fso = server. Createobject ("scripting. FileSystemObject ")
If err. Number <> 0 then
Err. Clear
Outputerr "error: the server does not support the FSO component. Download failed"
End if

'Get the file name and file size
Dim s_filemappath
Dim o_file, s_filename, n_filelength
S_filemappath = server. mappath (s_downfilepath)
If (o_fso.fileexists (s_filemappath) = true then
Set o_file = o_fso.getfile (s_filemappath)
S_filename = o_file.name
N_filelength = o_file.size
O_file.close
Else
Outputerr "error: file does not exist, download failed"
End if
Set o_fso = nothing

'Determine whether the size of the downloaded file exceeds the limit
'

'If you do not use stream download, directly go to the file
If use_stream = 0 then
Response. Redirect sdownfilepath
Response. End
End if

'Check whether the server supports ADODB. stream'
On Error resume next
Set o_stream = server. Createobject ("ADODB. Stream ")
If err. Number <> 0 then
Err. Clear
Outputerr "error: the server does not support the ADODB. Stream component. Download failed"
End if

O_stream.type = 1
O_stream.open
O_stream.loadfromfile s_filemappath

Response. Buffer = true
Response. Clear
Response. addheader "content-disposition", "attachment; filename =" & s_filename
Response. addheader "Content-Length", n_filelength
Response. charset = "UTF-8"
Response. contenttype = "application/octet-stream"
Response. binarywrite o_stream.read
Response. Flush

O_stream.close
Set o_stream = nothing

End Function

Sub outputerr (s_errmsg)
Response. Write "<font color = Red>" & s_errmsg & "</font>"
Response. End
End sub

%>
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.