ASP Implementation Web page open any type of file Save method

Source: Internet
Author: User
Tags file size client

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!



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.