ASP Implementation Web page open any type of file is prompted to save the method with code _ Application Tips

Source: Internet
Author: User
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!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.