Uploading ASP Component File objects
Currently, applications based on browser/server mode are more popular. When a user needs to transfer files to a server, one of the common methods is to run the FTP server and set the FTP default directory for each user to the user's Web home directory, so that the user can run the FTP client and upload files to the specified Web directory. This requires users to know how to use an FTP client. 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 with only a Web browser, this will be very convenient for them. However, since the file System object can only transfer text file limitations, the biggest problem with ASP is the problem of uploading files. Here's how to upload a file in a Web page based on an HTTP protocol.
A Three mechanisms for uploading over HTTP
There are three mechanisms for uploading via http: RFC1867, put, and WebDAV.
Put was introduced in HTTP 1.1 with a new HTTP verb. When the Web server receives an HTTP put and object name, it authenticates the user, receives the contents of the HTTP stream, and stores it directly into the Web server. Because this can cause damage to a Web site, it can also lose the most HTTP advantage: 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 to get your application to capture put is to operate at the lower level, the ISAPI filter layer. The application of put is limited due to the corresponding reasons.
WebDAV allows distributed authentication and translation of Web content. It introduces several new HTTP verbs that allow uploading via HTTP, locking/unlocking, registering/verifying Web content. The "Save to Web" in Office 2000 is implemented through WebDAV. If all you're interested in is uploading content, WebDAV is a great application and it solves a lot of problems. However, if you need to upload files in your Web application, WebDAV is useless to you. Like the HTTP put, those WebDAV verbs are interpreted by the server, not the Web application. You need to work on these verbs in the ISAPI filtering layer to access WebDAV and interpret the content in your application.
RFC1867 (Http://www.ietf.org/rfc/rfc1867.txt) was eventually accepted by the HTML3.2 as a recommended standard. It's a very simple but powerful idea: Define a new type in a form field.
<input type= "FILE" >
And in the form itself, a different encoding scheme is added, no longer using the typical:
<form action= "formproc.asp" method= "POST" >
Instead use:
<form action= "formproc.asp" method= "POST" enctype= "Multipart/form-data" >
This coding scheme is much more efficient when it transmits large amounts of data than the default "application/x-url-encoded" form coding scheme. URL encoding has only a limited set of characters, using any character that exceeds the character set, must be replaced with '%nn ', where nn represents the corresponding 2 hexadecimal digits. For example, even ordinary whitespace characters are replaced with '%20 '. RFC1867 uses a multipart MIME encoding, which is not encoded to transmit large amounts of data, as is often seen in e-mail messages, but simply adds a few simple but useful headers around the data. The main browser manufacturers have adopted the recommended "Browse ..." button, users can easily use the local "open file ..." dialog box to select the file to upload.
RFC1867 still leaves the flexible method of uploading most files to your Web application. Put is very limited. WebDAV is useful to authors of content, such as FrontPage users, but is rarely used by web developers who want to add file uploads to a Web application. Therefore, RFC1867 is the best way to add file uploads to a Web application.
In practical applications, Microsoft provides free Posting Acceptor. ASP does not understand the "multipart/form-data" coding scheme. Instead, Microsoft offers Posting acceptor, Posting acceptor an ISAPI application that accepts repost to an ASP page after the upload is complete.
Software Artisans's sa-fileup is one of the earliest commercial Active server components. After several improvements, it is now present as a pure ASP component.
Two Implementation principle analysis of file uploading based on ASP
The basic principle is: Use ADO Stream object's BinaryRead method to read out all the data in form, intercept the required file data and save it in binary file.
Here is an example of uploading a file page (upload.htm):
<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>
The file object is used in the program, so the raw data that is read by the BinaryRead method in the upload.asp is not only the data of the selected file itself, but also the description of the path, type, the form domain name of the file on the user's hard disk, and the related information. So we need to extract the specific contents of the file. According to the analysis, the data's header information and the data dividing line is two pairs of carriage return line character, the tail also has the separation information, we may use similar method to obtain the file data.
Dim Formdata.formsize,datastart,clstr,divstr
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
CLSTR=CHRB (&CHRB) (10)
DATASTART=INSTRB (FORMDATA.CLSTR&CLSTR) +4
' 4 is the length of the two-carriage return line feed
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 document.
In the middle according to the need, may carry on the corresponding processing. The last job is to save the file. There are two ways to save: One is the use of VB or VC programs such as binary file operation method, in the project to add the appropriate type library, and eventually compiled into a DLL file, use the DLL file to register it. File storage procedures are 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 code is the operation of file storage
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 (+) & 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 to it
Open Pathname for Binary as 1
Put #1, FormData.
Close #1
Savefile= "ok!"
End Function
The second method is done using the binary file operation method provided in the ADO stream, and the statement to save the file is: Streamobj.savetofile (filename,2). In this operation, we can store the relevant operations in a class file, in the application, directly to the class file included in the ASP program can be. For specific processing, see the introduction.