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 installed the office, click the link to call word to open the browser; when the client machine does not have office installed, click the link to bring up the Save dialog box. To sum up, if the browser knows the file type, it will automatically open; if not, it will prompt the customer to save. However, sometimes we do not want to open any type of files and directly store them on the client. To meet this requirement, you can use the sendbinary method of the aspupload component for files stored on the server's hard disk. For files stored in the database, you only need to open the record set, then, output the binary data directly to the client, but tell the browser the MIME type, file name, and file size of the file.
1. files on the server hard disk
CopyCode The Code is as follows: <%
Dim upload, filepath
Set upload = server. Createobject ("persits. Upload ")
Filepath = server. mappath (".") & "\" & "2003529213019.txt"
'Sendbinary parameter description:
'Parameter 1: physical file path
'Parameter 2: whether to transmit the MIME type and other information of the file to the browser
'Parameter 3: file type. The specific MIME type can be specified, but application/octet-binary can be generally used.
'Parameter 4: allow the client to 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 database Copy codeThe Code is as follows: <%
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") 'must be converted using CSTR
Response. binarywrite objrs ("file ")
Objrs. Close
Set objrs = nothing
Objconn. Close
Set objconn = nothing
%>
This method requires that the file name and size must be saved at the same time! If the file name and size are not specified, if the browser knows the file type, the file will be opened automatically. If not, the client will be prompted to save the file!