ASP prompts the saving method when opening any type of files on a webpage.

Source: Internet
Author: User

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!

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.