ASP components upload ---------

xiaoxiao2021-03-05  29

ASP component file object

Currently, the application based on the browser / server mode is popular. When the user needs to transfer files to the server, one of the common methods is to run the FTP server and set each user's FTP default directory to the user's web home directory, so that the user can run the FTP client and upload files to specified Web directory. This requires users to know how to use the FTP client. Therefore, this solution is only possible to familiarize with FTP and experienced users. If we can integrate file upload features with Web, users can use web browsers to complete upload tasks, which will be very convenient for them. However, since the file system object can only transfer the limitations of the text file, the biggest problem with the ASP is the file upload problem. The following describes how to upload the file in a web page based on HTTP protocol.

One. Three mechanisms uploaded by HTTP

Three mechanisms are uploaded by HTTP: RFC1867, PUT and WebDAV.

PUT is introduced in HTTP 1.1 into a new HTTP verb. When the web server receives an HTTP PUT and an object name, it will verify the user, receive the contents of the HTTP stream, and store it directly into the web server. Since this may cause damage to a Web site, and will lose HTTP's largest advantage: server programmability. In the case of PUT, the server handles the request: no space allows the CGI or ASP application to intervene. The only way to capture the PUT for your application is to operate at a low-level operation, the ISAPI filter layer. PUT's application is limited due to corresponding reasons.

WebDAV allows distributed certification and translation of Web content. It introduces several new HTTP verbs that allow for Upload, lock / unlock, register / test the web content via HTTP. "Save to Web" in Office 2000 is implemented by WebDAV. If everything you are interested is uploaded, WebDAV is very good, it solves a lot of problems. However, if you need to upload files in your web application, WebDAV is useless to you. Like HTTP PUT, those WebDAV verbs are interpreted by the server, not a web application. You need to work in the ISAPI filter layer to access these verbs of WebDAV and explain the content in your application.

RFC1867 (http://www.ietf.org/rfc/rfc1867.txt) was eventually accepted by W3C in HTML3.2, as a recommendation standard. It is a very simple but powerful idea: define a new type in the form field.

And in the form itself, different coding schemes are added, and typical:

But use:

This encoding scheme is more efficient than the default "Application / X-URL-Encoded" form encoding scheme in transmitting large amounts of data. The URL encoding is only a limited character set. Use any characters that exceed the character set, must be replaced with '% NN', where NN represents the corresponding 2 hexadecimal numbers. For example, even the ordinary space character is replaced with '% 20'. The RFC1867 uses multiple MIME encodings, just like it is often seen in the E-mail message, not encoding a large amount of data, but only adds little but simple but practical heads around the data. The main browser manufacturers have adopted the recommendation "Browse ..." button, and users can easily use the local "Open File ..." dialog to select the file you want to upload. RFC1867 still leaves the flexible method uploaded by most files to your web application. PUT is used very limited. WebDAV is very useful for the author of content, such as FrontPage users, but Web developers who want to join files in a web application are rarely used. Therefore, RFC1867 is the best way to join file uploads in a web application.

In practical applications, Microsoft offers Posting Acceptor. ASP does not understand the "Multipart / Form-Data" coding scheme. Instead, Microsoft provides Posting Acceptor, Posting Acceptor is an ISAPI application that accepts Repost to an ASP page after the upload is completed.

SA-Fileup of Software Artisans is one of the earliest commercial Active Server components. Several improvements, now exists as a purely ASP component.

two. Principle Analysis of File Uploading Based on ASP

The basic principle is to read all the data in the FORM in the BinaryRead method of the ADO Stream object, and intercept the required file data, and store the binary file.

Below is an example of uploading file pages (UPLOAD.HTM):

The file object is used in the program so that the original data read in the BinaryRead method in UPLOAD.ASP is not only the data of the selected file itself, but also contains the path, type, and submission page on the user's hard disk. The description of related information so that we need to extract the specific content of the file. According to the analysis, the header information of the data and the data of the data is two pairs of carriage return, and the tail also has separated information, we can use the following method to obtain file data.

Dim FormData.FormSize, DataStart, CLStr, DivStrFormSize = Request.TotalBytesFormData = Request.BinaryRead (FormSize) CLStr = ChrB (13) & ChrB (10) DataStart = InStrB (FormData.CLStr & CLStr) 4'4 are two pairs of carriage return linefeed Length divstr = Leftb (FormData, INSTRB (FormData, CLSTR) -1) DataSize = INSTRB (Datastart 1, Formdata, Divstr) -datastart-2formData = MIDB (FormData, Datast, DataSize) Formdata is the content of the file. The intermediate can be processed according to the needs. The final job is to save the file. There are two ways to save: one is to use the binary operation method in the VB or VC program, add the appropriate type library in the project, and finally compile into a DLL file, and then register the DLL file when used. . The file storage program 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 a file stored on the operation 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, Datast, DataSize) 'creation A binary file and writes Formdata Open Pathname for binary as 1 Put # 1, Formdata Close # 1 Savefile = "OK!" End function

The second method is to complete the binary operation method provided in the ADO stream, the statement to save the file is: streamobj.savetofile (filename, 2). In this operation, we can put the relevant operations in a class file, and it is possible to include this class file directly in the ASP program when applying. For specific processing methods, please refer to the relevant introduction.

转载请注明原文地址:https://www.9cbs.com/read-37270.html

New Post(0)