I believe everyone has this experience: there is a link on the page pointing to a Word file on the server. When the client machine has Office installed, clicking the link will call Word to open and browse; when the client machine does not have Office installed, clicking the link will pop up. Save dialog box. To sum up, if the browser recognizes the file type, it will automatically open it; if it does not, it will prompt the customer to save it. But sometimes we hope that no matter what type of file, we don't want to open it and let the client save it directly. To achieve this requirement, for files saved on the server's hard disk, the SendBinary method of the ASPUpload component must be used. For files saved in the database, you only need to open the record set and then output the binary data directly to the client. , but you need to tell the browser the MIME type, file name and file size of the file.
1. Files on the server’s hard drive
<%
Dim Upload,FilePath
Set Upload = Server.CreateObject(Persits.Upload)
FilePath = Server.MapPath(.) & / & 2003529213019.txt
'SendBinary parameter description:
'Parameter 1: File physical path
'Parameter 2: Whether to send the MIME type and other information of the file to the browser
'Parameter three: file type, you can specify a specific MIME type, but generally you can use application/octet-binary
'Parameter 4: Let the client save the file or open it directly. True: save; False (default): open
Upload.SendBinary FilePath,True,application/octet-binary,True
%>
2. Files in the server-side database
<%
Dim objConn,objRs
Set objConn = Server.CreateObject(ADODB.Connection)
Set objRs = Server.CreateObject(ADODB.RecordSet)
objConn.open Driver={Microsoft Access Driver (*.mdb)};dbq= & Server.MapPath(db1.mdb)
objRs.open select * from t5 where id=2,objConn,1,3
Response.ContentType = application/octet-stream
Response.AddHeader Content-Disposition,attachment;filename= & objRs(filename)
Response.AddHeader Content-Length,CStr(objRs(size)) 'CStr conversion must be used here
Response.BinaryWrite objRs(file)
objRs.close
Set objRs = nothing
objConn.close
Set objConn = nothing
%>
This method requires that the file name and file size must be saved at the same time when saving the file! If the file name and size are not specified, if the browser recognizes the file type, it will automatically open it; if it does not, the client will be prompted to save it!