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 the save dialog box. To sum up, if the browser knows the file type, it will automatically open; if it doesn't know, the customer will be prompted to save. But sometimes we hope that no matter what type of file, we will not open it and let the client save it directly. To meet this requirement, the files saved on the server hard disk must be implemented using the SendBinary method of the ASPUpload component. For files stored in the database, you only need to open the record set and output the binary data directly to the client. However, you need to tell the browser file's MIME type, file name and file size.
1. Files on the server side hard drive
The code copy is as follows:
<%
DimUpload, FilePath
SetUpload=Server.CreateObject("Persits.Upload")
FilePath=Server.MapPath(".")&"/"&"2003529213019.txt"
'SendBinary parameter description:
'Parameter 1: File physical path
'Parameter 2: Whether to transfer information such as MIME type of the file to the browser
'Parameter 3: File type, you can specify the 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.SendBinaryFilePath,True,"application/octet-binary",True
%>
2. Files in the server-side database
The code copy is as follows:
<%
DimobjConn,objRs
SetobjConn=Server.CreateObject("ADODB.Connection")
SetobjRs=Server.CreateObject("ADODB.RecordSet")
objConn.open"Driver={Microsoft AccessDriver(*.mdb)};dbq="&Server.MapPath("db1.mdb")
objRs.open"select*fromt5whereid=2",objConn,1,3
Response.ContentType="application/octet-stream"
Response.AddHeader"Content-Disposition","attachment;filename="&objRs("filename")
Response.AddHeader"Content-Length",CStr(objRs("size"))' Here you must convert with CStr
Response.BinaryWriteobjRs("file")
objRs.close
SetobjRs=nothing
objConn.close
SetobjConn=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 knows the file type, it will be opened automatically; if it does not know, the customer will be prompted to save!