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, server-side hard disk files
The following are the referenced contents: <% 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 database
The following are the referenced contents: <% 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")) ' here must be converted by 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!