ASP file upload Principle Analysis and Implementation example

Source: Internet
Author: User
Tags ftp client how to use ftp
Abstract: In A/-based application environment, uploading various types of files has always been one of the challenges that plague user 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 object. This article discusses the upload methods and implementation principles and provides specific solutions.

ASP file object

Currently, applications based on/mode are quite popular. When you need to transfer files to, one of the common methods is to run FTP and set the default FTP directory of each user to your web home directory, in this way, you can run the FTP client program and upload files to the specified web directory. This requires users to understand how to use FTP client programs. Therefore, this solution is only feasible for users who are familiar with FTP and are experienced. If we can integrate the file upload function with the Web so that users can complete the upload task only on the web, it will be very convenient for them. However, since file system object can only transmit text files, ASP's biggest problem is file upload. The following describes how to upload files on an HTTP-based webpage.

I. Three Mechanisms for uploading via HTTP

HTTP upload has three mechanisms: rfc1867, put, and WebDAV.

Put introduces a new HTTP verb in HTTP 1.1. When the Web receives an http put and Object Name, it verifies the user, receives the HTTP stream content, and directly saves it to the Web. This may cause damage to a Web site, and also lose the biggest advantage of http: programmability. In the case of put, handle the request by yourself: there is no space for CGI or ASP applications to intervene. The only way for your application to capture put is at the lower-layer operation and ISAPI filtering layer. Due to the corresponding reasons, the put application is very limited.

WebDAV allows Distributed Authentication and translation of web content. It introduces several new HTTP verbs that allow uploading, locking/unlocking, registering/verifying web content over HTTP. In Office 2000, "Save to Web" is implemented through WebDAV. If everything you are interested in is uploading content, the WebDAV application is excellent and solves many 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, not web applications. You need to work on the ISAPI filter layer to access these verbs of WebDAV and explain the content in your application.

Rfc1867 (http://www.myshui.cn/book/rfc/RFC1867.txt) was finally accepted by W3C in html3.2 as a recommended standard. It is a very simple but powerful idea: define a new type in form fields.
<Input type = "file">

In addition, different encoding schemes are added to the form itself, and the typical ones are no longer used:
<Form action = "formproc. asp" method = "Post">

Instead, use:
<Form action = "formproc. asp" method = "Post" enctype = "multipart/form-Data">

This encoding scheme is much more efficient than the default "application/X-URL-encoded" form encoding scheme when transmitting a large amount of data. URL encoding only has a limited character set. Any character that exceeds the character set must be replaced by '% nne'. Here, NN represents two hexadecimal numbers. For example, even common space characters must be replaced by '% 20. Rfc1867 uses multi-part mime encoding, as we usually see in the E-mail message, instead of encoding to transmit a large amount of data, however, a few simple but practical headers are added around the data. Major vendors have adopted the suggested "Browse..." button. You can easily use the local "open file..." dialog box to select the file to be uploaded.

Rfc1867 still leaves flexible methods for uploading most files to your web application. Put is rarely used. WebDAV is very useful for content authors, such as FrontPage users, but it is rarely used by web developers who want to add file uploads to Web applications. Therefore, rfc1867 is the best way to add files to the Web application.

In practical applications, Posting Acceptor is provided for free. ASP does not understand the "multipart/form-Data" encoding scheme. Instead, Posting Acceptor is provided. Posting Acceptor is an ISAPI application that accepts repost to an ASP page after the upload is complete.

The SA-fileup of software artisans is one of the earliest commercial active servers. After several improvements, it is now a pure ASP.

Ii. Analysis of ASP-based file upload implementation principles

The basic principle is to use the binaryread method of the ADO Stream object to read all the data in the form, extract the required file data, and save the disk as a binary file.

The following is an example (upload.htm) of the file uploading page ):
<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. the raw data read by using the binaryread method in ASP is not only the data of the selected file, it also contains descriptions of the path, type, form of the submitted page, and other related information of the file on the user's hard disk. In this way, we need to extract the specific content of the file. According to the analysis, the line between the data header information and the data is two carriage return linefeeds with separated information at the end. We can use the following methods to obtain the file data.
Dim formdata. formsize, datastart, clstr, divstr
Formsize = request. totalbytes
Formdata = request. binaryread (formsize)
Clstr = chrb (13) & chrb (10)
Datastart = rule B (formdata. clstr & clstr) + 4
'4' is the length of two line breaks
Divstr = leftb (formdata, Role B (formdata, clstr)-1)
Datasize = synchronized B (datastart + 1, formdata, divstr)-DataStart-2
Formdata = midb (formdata, datastart, datasize)

Formdata is the content of the file.

You can perform corresponding processing as needed. The final task 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 in the project, and finally compile it into a DLL file, you can register the DLL file when using it. 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 code is related to 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 (13) & chrb (10)
Datastart = instrb (formdata, clstr & clstr) + 4
Divstr = leftb (formdata, Role B (formdata, clstr)-1)
Datasize = synchronized B (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 to use the binary file operation method provided in ADO stream. The statement for saving the file is streamobj. savetofile (filename, 2 ). In this operation, we can store the related operations in a class file. During the application, we can directly include the class file in the ASP program. For more information, see.

Iii. Example of File Upload Implementation Method

You can use or use this method to upload files. For classes, such as Microsoft Posting Acceptor (MPA), it is a free product released by the company. Such installation is also convenient. For the MPa, run the installation file. In the general DLL format, we need to register. For example, to use aspcnup. DLL, As long as regsvr32 [path/] aspcnup is executed on Window 2000. DLL, the system prompts that the registration is successful and you can use it. For non-class, such as http://www.5xsoft.com/, there is no upload class-upload_5xsoft. In use, you only need to include the following statements in the processing program:
<! -- # Include file = "Upload. Inc" -->
Set upload = new upload_5xsoft 'create an upload object

For more information about the attributes and operation methods, see this user manual.

For example, upload the source code (upload. asp) of some types of files ):
<% @ 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

%>

Iv. Conclusion

The application mode is still developing rapidly. In ASP. NET, the file upload function has been built in, which is very simple and convenient to use. As a new technology, ASP. NET is not just a simple upgrade of ASP. It is a new framework for web development, including many new features. ASP. NET provides code that is easier to write and clearer in structure. With this code, we can reuse and share it more easily to develop more practical programs.

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.