I believe you usually have this experience: the page has a link to the server A Word file, when the client machine has installed Office, click on the link will call Word open browse; When the client machine does not have Office installed, clicking on the link will pop up the Save dialog box. To sum up, if the browser knows the file type, it will open automatically, and if not, the customer will be prompted to save. But sometimes we want no matter what type of file, do not open, directly to the client to save. To achieve this requirement, files saved on the server's hard disk have to be implemented using the Sendbinary method of the AspUpload component, while the recordset must be opened for the files saved in the database, and the binary data is then exported directly to the client. But tell the browser the MIME type, file name, and file size of the file.
1. Files on server-side hard disks
Copy Code code as follows:
<%
Dim Upload,filepath
Set Upload = Server.CreateObject ("Persits.upload")
FilePath = Server.MapPath (".") & "\" & "2003529213019.txt"
' Sendbinary parameter Description:
' Parameter one: file physical path
' parameter two: whether to pass information such as MIME type of file to the browser
' Parameter three: file type, you can specify a specific MIME type, but you can generally use the application/octet-binary
' Parameter four: 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 server-side databases
Copy Code code as follows:
<%
dim objconn,objrs
set objconn = server.createobject ("ADODB.") Connection ")
Set objrs = server.createobject (" ADODB. RecordSet ")
objconn.open " driver={microsoft access driver (*.mdb)};d bq= " & 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")) ' This must be converted with CStr
Response.binarywrite objrs ("file")
objrs.close
set objrs = nothing
objconn.close
set objconn = nothing
%>
This method requires that you save the file name and file size at the same time! If you do not specify a filename and size, if the browser recognizes the file type, it will automatically open, if you do not know, will prompt the customer to save!