Using response to access files in ASP

Source: Internet
Author: User

I am using ASP for a unit to create a Web page encountered such a problem, the unit before the MIS system in a number of Word files in the form of a byte stream saved in the database, and now the user asked me to use ASP to remove these Word file data from the database and displayed in the Web page. At first I naturally thought about creating temporary files on the server, and then adding a link to the temporary file in the Web page, but this method would greatly increase the burden on the server and how to ensure that the temporary files used by a particular client are not overwritten by files used by other clients on the service. How to delete files after they are delivered to the user is difficult to solve in practice. So is there a better way?

So I looked at the ASP's reference books and found that the response object has a property called ContentType that defines the MIME type that the server sends to the client's content. The MIME full name Multipurpose Internet Mail Extensions, the Multipurpose Internet Mail extension. We know that in web programming we sometimes point hyperlinks to a Word or Excel file, and when the user clicks on the link, the browser automatically invokes the corresponding method to open the file. This is done because the corresponding MIME resource type is registered in the browser when Office is installed on the user's machine. For example, the MIME type of a Word file is Application/msword (the former is a MIME type, the latter is a mime subclass), and the MIME resource type of the Excel file is application/msexcel. In fact, all the resources that the browser can handle have the corresponding MIME resource type, for example, the MIME type of the HTML file is the MIME type of the Text/html,jpg file is image/jpg. In the interaction with the server, the browser is based on the data to accept the MIME type to determine what to do, the HTML, JPG, and other file browsers open it directly to the Word, Excel and other browsers themselves can not open the file calls the appropriate method to open. For files that do not have a marked MIME type, the browser guesses its type based on its extension and file contents. If the browser cannot guess, use it as a application/octet-stream. To learn the MIME types of various files, view them in the Win98 my Computer-> View-> Folder Options-> File type.

So I brainwave, think in the ASP can first the word data in a byte-throttling way out, then mark its Conntenttype property as Application/msword, and then send it to the client, after the client received the resource, according to its MIME type, will automatically invoke word on the client (if, of course, the client has word on it, it will be opened as an unrecognized resource, prompting the user to save instead of opening it). The experiment worked well, the method was simple and fast, and the browser was opened with inline (similar to OLE) in IE5, which worked better. The following is the program content.

Suppose the table name Tab_word, there are two fields in the table, one is an integer, the name ID, used as a unique identifier for Word data, another BLOB type, name Worddata, which holds word data. Now to display the contents of the Word file with ID equal to 1 on the page, the ASP program is as follows:

< %
' conn - 已创建的数据库连接
' rs -- 结果集
rs = conn.execute("select
worddata from tab_word where id = 1")
response.contenttype = "Application/msword"
response.writebinary(rs("worddata"))
'注意将结果集中的数据直接用writebinary发送出去,不要用变量
'接收这个数据,否则系统会报错
% >

In a similar way, you can also work with many types of data, such as Excel, BMP, and so on.

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.