Custom file downloads in ASP

Source: Internet
Author: User
Downloads can be downloaded (memory, less) or go directly to the file.


<%

Constuse_stream=0 ' 0. No flow (adodb.stream) Download 1. Use the Shed to load
constallow_file_ext= "Rar,zip,chm,doc,xls,swf,mp3,gif,jpg,jpeg,png,bmp" ' allows downloading of file extensions to prevent source code from being downloaded

Dimsdownfilepath ' Download file path
Sdownfilepath=trim (Request ("FilePath"))
' or get the file path from the database based on the file ID passed in.

' If Sdownfilepath is an absolute path, be sure to convert the Sdownfilepath to a relative path relative to this file

' Sdownfilepath= ' focus.swf '

Calldownloadfile (Sdownfilepath)

Functiondownloadfile (S_downfilepath)
' Judge if there is no pass file name
Ifisnull (S_downfilepath) =trueortrim (S_downfilepath) = "" Then
Outputerr "ERROR: First determine the file to download, download failed"
EndIf

' Determine if the extension is legitimate
Dims_fileext
S_fileext=mid (S_downfilepath,instrrev (S_downfilepath, ".") +1)
Ifinstr ("," &ALLOW_FILE_EXT& ",", "," &s_FileExt& ",") <=0then
Outputerr "Error: File type (" &s_FileExt& ") is not allowed to be downloaded, download failed"
EndIf

S_downfilepath=replace (S_downfilepath, "", "/")

' For security, some directories prohibit downloading files, processing here
'

' Detect if the server supports FSO
Dimo_fso
Onerrorresumenext
Seto_fso=server.createobject ("Scripting.FileSystemObject")
Iferr.number<>0then
Err.Clear
Outputerr "Error: Server does not support FSO component, download failed"
EndIf

    ' get filename, file size
   dims_filemappath
   dimo_file,s_ Filename,n_filelength
   s_filemappath=server.mappath (s_downfilepath)
    If (O_fso.fileexists (S_filemappath)) =truethen
       seto_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"
   endif
   seto_fso=nothing

' Determines whether the downloaded file size exceeds the limit
'

' If it's not streamed, go directly to the file
Ifuse_stream=0then
Response.redirectsdownfilepath
Response.End
EndIf

' Detect if the server supports ADODB.stream
Onerrorresumenext
Seto_stream=server.createobject ("ADODB.stream")
Iferr.number<>0then
Err.Clear
Outputerr "Error: Server does not support ADODB.stream component, download Failed"
EndIf

O_stream.tyep=1
O_stream.open
O_stream.loadfromfiles_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.BinaryWriteo_Stream.Read
Response.Flush

O_stream.close
Seto_stream=nothing

Endfunction

Suboutputerr (S_ERRMSG)
Response.Write "<fontcolor=red>" &s_ErrMsg& "</font>"
Response.End
Endsub

%>

Http://www.cnblogs.com/jiny-z/archive/2006/08/29/489102.html



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.