Abstract: In a browser/server-based application environment, uploading various types of files in the browser has always been one of the problems that plagues users' file management applications. There are three mechanisms for uploading files in HTTP: RFC1867, PUT and WebDAV. A common implementation method is to use a new type introduced in RFC1867: File and ADO Stream objects. This article discusses the above upload methods and implementation principles, and gives specific solution examples.
Keywords: ASP component FILE object
Currently, applications based on the browser/server model are more popular. When users need to transfer files to the server, one of the common methods is to run an FTP server and set each user's FTP default directory as the user's Web home directory, so that the user can run the FTP client program and upload files to the specified Web directory. This requires users to know how to use the FTP client program. Therefore, this solution is only feasible for experienced users who are familiar with FTP. If we can integrate the file upload function with the Web so that users can complete the upload task using only a Web browser, it will be very convenient for them. However, due to the limitation that File System Object can only transmit text files, the biggest problem of ASP is file uploading. The following describes how to upload files in a web page based on the HTTP protocol.
one. Three mechanisms for uploading via HTTP
There are three mechanisms for uploading via HTTP: RFC1867, PUT and WebDAV.
PUT is a new HTTP verb introduced in HTTP 1.1. When the web server receives an HTTP PUT and the object name, it authenticates the user, receives the contents of the HTTP stream, and stores it directly into the web server. Because this can be disruptive to a web site, it also loses the biggest advantage of HTTP: server programmability. In the case of PUT, the server handles the request itself: there is no room for CGI or ASP applications to intervene. The only way for your application to capture PUTs is to operate at a low level, the ISAPI filtering layer. For corresponding reasons, the application of PUT is very limited.
WebDAV allows distributed authentication and translation of web content. It introduces several new HTTP verbs that allow uploading, locking/unlocking, and checking in/checking web content over HTTP. "Save to web" in Office 2000 is implemented through WebDAV. If all you're interested in is uploading content, WebDAV works great and solves a lot of problems. However, if you need to upload files within your web application, WebDAV is of no use to you. Like HTTP PUT, those WebDAV verbs are interpreted by the server, not the web application. You need to work on the ISAPI filtering layer to access these verbs of WebDAV and interpret the content in your application.
RFC1867 ( http://www.ietf.org/rfc/rfc1867.txt ) was used as a proposed standard before it was finally accepted by the W3C in HTML3.2. It's a very simple but powerful idea: define a new type in a form field.
<INPUT TYPE="FILE">
And added different encoding schemes to the form itself, instead of using the typical:
<FORM ACTION="formproc.asp" METHOD="POST">
but using:
<FORM ACTION="formproc .asp" METHOD="POST" ENCTYPE="multipart/form-data">
This encoding scheme is more efficient than the default "application/x-url-encoded" form encoding scheme when transmitting large amounts of data. much higher. URL encoding has only a very limited character set. Any characters that exceed the character set must be replaced by '%nn', where nn represents the corresponding 2 hexadecimal digits. For example, even ordinary space characters should be replaced with '%20'. RFC1867, on the other hand, uses multi-part MIME encoding, as commonly seen in e-mail messages, to transmit large amounts of data without encoding, but with only a few simple but useful headers surrounding the data. Major browser manufacturers have adopted the recommended "Browse..." button, and users can easily use the local "Open File..." dialog box to select files to upload.
RFC1867 still leaves most of the flexible methods of file uploading to your web application. PUT has very limited use. WebDAV is useful for content authors, such as FrontPage users, but is less useful for web developers who want to incorporate file uploads into their web applications. Therefore, RFC1867 is the best way to include file upload in web applications.
In practical applications, Microsoft provides Posting Acceptor for free. ASP does not understand the "multipart/form-data" encoding scheme. Instead, Microsoft provides Posting Acceptor, an ISAPI application that accepts a REPOST to an ASP page after the upload is complete.
Software Artisans' SA-FileUp was one of the first commercial Active Server components. After several improvements, it now exists as a pure ASP component.
two.
The basic principleof ASP-based file upload implementation principle analysis
is: use the BinaryRead method of the ADO Stream object to read out all the data in the FORM, intercept the required file data, and save it as a binary file.
Here is an example of a file upload page (upload.htm):
<html>
<body>
<form name="Upload" Method="Post" Enctype="multipart/form-data" Action="Upload.asp">
<input type="file" name="FileName">
<INPUT TYPE="Submit" VALUE="Upload"></TD>
</form>
</body>
</html>
The file object is used in the program, so that the original data read using the BinaryRead method in Upload.asp is not only the data of the selected file itself, but also includes the path, type and submission of the file on the user's hard disk. Description of the form domain name and other related information of the page, so that we need to extract the specific content of the file. According to the analysis, the dividing line between the header information and the data is two pairs of carriage returns and line feeds, and there is also separation information at the end. We can obtain the file data using a method similar to the following.
Dim FormData.FormSize,DataStart,CLStr,DivStr
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize)
CLStr=ChrB(13)&ChrB(10)
DataStart=InStrB(FormData.CLStr&CLStr)+4
'4 is the length of two pairs of carriage return and line feed characters
DivStr=LeftB(FormData,InStrB(FormData,CLStr)-1)
DataSize=InStrB(DataStart+1,FormData,DivStr)-DataStart-2
FormData=MidB(FormData,DataStart,DataSize)
FormData is the content of the file.
Corresponding processing can be carried out as needed. The last thing to do is to save the file. There are two ways to save: one is to use the binary file operation method in programs such as VB or VC, add the appropriate type library to the project, and finally compile it into a DLL file, and then register the DLL file when using it. . The file storage procedure is as follows:
Public Function SaveFile(Pathname As String) As String
Dim objContext As ObjectContext
Dim objRequest As Request
Set objContext=GetObjectContext()
Set objRequest=objContext("Request")
'The following piece of code is related to file storage operations Dim FormData() As Byte, CLStr, DivStr
Dim DataStart As Long,DataSize As Long
DataSize=objRequest.TotalBytes
Redim FormData(DataSize-1)
FormData=objRequest.BinaryRead(DataSize)
CLStr=ChrB(13) & ChrB(10)
DataStart=InStrB(FormData,CLStr & CLStr)+4
DivStr=LeftB(FormData,InStrB(FormData,CLStr)-1)
DataSize=InStrB(DataStart+1,FormData,DivStr)-DataStart-2
FormData=MidB(FormData,DataStart,DataSize)
'Create a binary file and write FormData into it Open Pathname For Binary As 1
Put #1,,FormData
Close #1
SaveFile="OK!"
of End Function
is to use the binary file operation method provided in ADO STREAM. The statement to save the file is: StreamOBJ.SaveToFile (fileName,2). In this kind of operation, we can store the relevant operations in a class file, and when applying it, just include the class file directly in the ASP program. For specific processing methods, please refer to the relevant introduction.
three. File upload implementation method instance
can use component or componentless method to implement file upload. For component categories, for example, Microsoft Posting Acceptor (MPA for short) is a free server component released by Microsoft. The installation of this type of component is also relatively convenient. For Microsoft's mpa, just run its installation file. For general dll components, we need to register them. For example, to use aspcnUP.dll, you only need to execute regsvr32 [path]aspcnUP.dll on Window 2000, and the system will prompt you with a successful registration message and you can use the component; for component-less classes, such as www.5xsoft.com 's Component upload class-upload_5xsoft. When using it, just include the following statement in the handler:
<!--#include FILE="upload.inc"-->
For the properties and operation methods related to
creating an upload object
, please refer to the user manual of this component.The following is the source code for uploading some types of files (upload.asp), taking the aspcnUP.dll component as an example:
<% @ language="vbscript"
Set fileUP=Server.CreateObject("aspcn.Upload")
fileUP.Maxsize=200000
fileUP.Path="d:upfile"
fileUP.Upload
For i=0 to fileUP.Count
fieldname=fileUP.FieldName(i)
If fileUP.FileType(fieldname)="zip" Or ileUP.FileType(fieldname)="rar" Then
fileUP.Save fieldname
End If
Next
Set fileUP=Nothing
%>
Four. Conclusion
The browser/server application model is still developing rapidly. In Microsoft's newly launched ASP. NET has built-in file upload function, which is very simple and convenient to use. As a brand-new technology, ASP. NET is not just a simple upgrade of ASP, it is a brand new framework for Web development that contains many new features. ASP. NET provides code that is easier to write and has a clearer structure. Using these codes, we will more easily reuse and share them, thereby developing more and more practical programs.