Using response attribute skillfully in ASP--contenttype

Source: Internet
Author: User
Tags mail resource table name client
Response I am in the use of ASP for a unit to create a Web page encountered such a problem, the unit before the MIS system in some Word files in the form of a byte stream saved in the database, 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?

----for this I looked over 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 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
, it is used as a application/octet-stream. To learn about the MIME types of various files, see the-> Folder Options-> file type in Win98 My Computer->.

----So I brainwave, think in ASP can first take Word data out of a byte stream, 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 is good, the method is simple and fast, and in IE 5 browsers use inline (similar to OLE Way) to open, the effect is better. The following is the program content.

----Assuming the table name Tab_word, there are two fields in the table, one is an integral type, the name ID, used as the unique identifier for Word data, and the other blob type, named 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-database connection created
' RS--result set
rs = Conn.execute ("Select
Worddata from Tab_word where id = 1 ")
Response.ContentType = "Application/msword"
Response.writebinary (RS ("Worddata"))
' Note that the data in the result set is sent directly with writebinary, not with variables
' Receive this data, or the system will complain
% >



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.