I encountered such a problem when using ASP to create a web page for a certain unit. The unit's previous MIS system saved some Word files in the form of byte streams in the database. Now the user requires me to use ASP to extract these Word file data from the database. taken out and displayed on the web page. At first, I naturally thought of creating a temporary file on the server, and then adding a link to the temporary file in the web page. However, this method will greatly increase the load on the server, and how to ensure that the temporary file used by a specific client is ensured on the service. The files are not overwritten by files used by other clients, and how to delete the files after they are transferred to the user. These problems are difficult to solve in practice. So is there a better way?
For this reason, I carefully checked the ASP reference book and found that the Response object has an attribute called contenttype, which defines the MIME type of the content sent by the server to the client. The full name of MIME is Multipurpose Internet Mail Extensions, which is a multi-purpose Internet mail extension. We know that in web programming we sometimes point a hyperlink to a Word or Excel file. When the user clicks the link, the browser will automatically call the corresponding method to open the file. The reason why this is possible is that after installing office on the user's machine, the corresponding MIME resource type will be registered in the browser. For example, the MIME type of a word file is Application/msword (the former is a MIME type and the latter is a MIME subclass), and the MIME resource type of an Excel file is Application/msexcel. In fact, all resources that the browser can handle have corresponding MIME resource types. For example, the MIME type of html files is Text/html, and the MIME type of JPG files is Image/JPG. In the interaction with the server, the browser determines what kind of processing is to be performed based on the MIME type of the received data. For files such as html and JPG, the browser will directly open them. For files such as Word and Excel, the browser cannot open them by itself. The file is opened by calling the corresponding method. For files that are not marked with a MIME type, the browser guesses its type based on its extension and file content. If the browser can't guess it, it passes it as application/octet-stream. To understand the MIME types of various files, please view them in win98 My Computer->View->Folder Options->File Types.
So I had an idea and thought that in ASP, I could first take out the WORD data as a byte stream, then mark its contenttype attribute as Application/msword, and then send it to the client. After the client receives this resource, it will MIME type, will automatically call Word on the client computer (of course, the premise is that Word is installed on the client computer, otherwise it will be treated as an unrecognized resource, prompting the user to save it instead of opening it) to open it. The test results are very good, the method is simple and fast, and the browser uses the embedded mode (similar to the OLE mode) to open it in IE5, and the effect is even better. The following is the program content.
Assume that the table name is tab_word. There are two fields in the table. One is an integer named id, which is used as the unique identifier of Word data. The other is a Blob type named worddata, which stores Word data. Now we want to display the content of the Word file with ID equal to 1 on the page. The ASP program is as follows:
< %
' conn - the 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))
'Be careful to send the data in the result set directly using writebinary, do not use variables
'Receive this data, otherwise the system will report an error
%>
Using similar methods, you can also process Excel, Bmp and many other types of data.