Principles of Web file upload

Source: Internet
Author: User
Using WEB technology to implement B/S (Browser/Server) structure management systems is the development trend of office automation. A web-based management system has a short development cycle and has nothing to do with the user platform. It is easy to implement interactive applications. It can quickly and efficiently collect, process, and publish information, it has developed rapidly in recent years. ASP has become the preferred tool for development and management systems due to its high development efficiency, good interaction, and high security.
Many Web-based applications involve file upload operations. Common File Upload technologies include: Based on HTTP protocol; based on VB (or Delphi, etc. Programming Language ) Developed File Upload components, database-based technologies, and so on. These methods generally require programmers to be able to master the Web technology, database technology, CGI technology or component technology at the same time, and have high requirements on programmers. This article will introduce the use of ASP technology to directly implement the file upload function, you only need the programmer to master a single ASP technology, greatly reducing the programming difficulty.
Comparison of several File Upload Technologies
1. HTTP-based
This method requires programmers to use third-party software, such as Delphi and VB. Program First, program the HTTP protocol, then package the content of the file to be uploaded in the HTTP format, and finally send the upload request message to the Web server to upload the file. Because delphi and VB cannot write a complete Web network program, they can only write small Web applications. Therefore, this method is only used for network applications with limited functions.
2. File Upload Component developed based on VB (or Delphi)
This method uses VB (or Delphi programming language) to Develop ASP server components to implement specific File Upload services. It first uploads files (in binary format) from the user end to the server using the ASP form function, and then processes the binary files using components developed by VB to become files that can be read and written normally. This method requires programmers not only to master ASP language, but also to use VB and other third-party languages for component programming, which increases the difficulty of development.
3. Database-based technology
This method is similar to the previous method. The difference lies in the processing of uploaded binary files. It uses a database to save binary files. Both small databases and large databases provide data types for storing binary data, as long as the data is stored in the corresponding field in Append Chunk mode. This method is simple and feasible, but because the size of each File Uploaded is different, it will cause a great waste of database space and reduce the data access speed; in addition, files can only be accessed in the database environment, causing great inconvenience.
Instance analysis
However, using ASP technology to directly implement the file upload function requires programmers to master a single ASP technology,
This greatly reduces programming difficulty. Next we will introduce how to use this method through an instance.
1. File Upload form
First, you need to write a form that provides the file upload function. The program is as follows:
<Form action = "Upload. asp" method = post enctype = "multipart/form-Data">
Upload File: <input type = file name = file1> <br>
<Input type = submit name = upload value = "Upload">
</Form>
The enctype parameter is used to set the form's mime encoding method. When uploading a file (or containing a text box at the same time), you must set its attribute to "multipart/form-Data"; upload. ASP is an ASP program used by the server to process received binary file streams. It will be introduced later in this article.
2. Analysis of uploaded file formats
Before processing a file, we must first understand the specific format of the uploaded file. By writing the following simple ASP program, we can view its binary Code :
<%
Filesize = request. totalbytes 'to get the size of the uploaded file
Filedata = request. binaryread (filesize) 'to obtain the binary data of the uploaded file
Response. binarywrite filedata: Display binary data in the browser
%>
Analyze the binary code of the uploaded file displayed on the browser, and find that the Code consists of four parts (if multiple files or text boxes are uploaded at the same time, the Code is sorted in the order of uploading, and the format is the same ), the content of each part is separated by a carriage return line break:
1) Part 1 (Start mark)
----------------------------- 7d329631b04d4
2) Part 2 (document description)
Content-Disposition: Form-data; name = "file1"; filename = "C: \ Documents ents and Settings \ Administrator \ My Documents ents \ invitation.doc" Content-Type: Application/MSWord
Here, we can obtain the name and absolute path of the file to be uploaded, or the file type. This information is indispensable for correctly saving files.

3) Part 3 (File Content)
That is, the binary content of the file, omitted.
4) Part 4 (ending mark)
----------------------------- 7d329631b04d4
According to the content in part 1 and part 4, "--------------------------- 7d329631b04d4" (each upload has different values) plays a role in the delimiter, it indicates the start and end of a piece of data (when there are multiple uploaded content. In terms of the information required to save the file, we first need to obtain the file name from the "FILENAME" of the second part of the data, and then need to correctly locate the start position of the file, finally, the binary file can be saved with the original file name using ASP technology. If multiple content (such as multiple text boxes and files) are uploaded at the same time, each part of the content is included in the delimiter, the text box and file are displayed in a slightly different way, which can be understood by analyzing its binary code.
3. Use ASP technology for file storage
Processing of uploaded file code
1) Get the delimiter code
From the above analysis, we know that the delimiter plays an important role in splitting multiple data segments (including text boxes and various types of files. As analyzed earlier, the delimiter appears before the first carriage return line break. Therefore, you can use the following program to obtain the delimiter code:
<%
Newline = chrb (13) & chrb (10) 'newline indicates the binary carriage return.
Filesize = request. totalbytes 'filesize is the size of the uploaded file
Filedata = request. binaryread (filesize) 'filedata is the binary data of the uploaded file.
Divider = leftb (filedata, clng (partition B (filedata, newline)-1) 'divider is the delimiter
%>
Note: Binary bytecode is processed here. Therefore, all functions use its binary version and add "B ".
2) Get the file (or text box) Content
(1) Preparation function (convert a binary string into a string)
The content of the uploaded file does not need to be converted from binary to string, and can be saved directly. However, if you want to extract the text box content or file name, you must convert it. Therefore, you need to write a general conversion function that is applicable to Chinese characters. The code for this function is as follows:
Function btos (BSTR)
If not is null (BSTR) then
For I = 0 to lenb (BSTR)-1
BCHR = midb (BSTR, I + 1, 1)
If ASCB (BCHR)> 127 then' is a dual-byte Chinese character, which must be processed together with two characters
Temp = temp & CHR (ASCW (midb (BSTR, I + 2, 1) & BCHR ))
I = I + 1
Else
Temp = temp & CHR (ASCB (BCHR ))
End if
Next
End if
Btos = temp
End Function
(2) Get the file (or text box) Content
In actual Web applications, the upload operation may involve multiple items, such as multiple text boxes and files. The file and the text box are distinguished. The file data contains the "filename =" string. Therefore, we have written the following general functions, which can be used to extract file content and text box content (Binary Conversion is required ):
Function getdata (byval data, byval divider, final) 'Data indicates a binary string; divider indicates a delimiter; Final indicates the end position of the data.
Filename = chrb (102) & chrb (105) & chrb (108) & chrb (101) & chrb (110) & chrb (97) & chrb (109) & chrb (101) & chrb (61) & chrb (34) 'string "FILENAME" binary representation
Bncrlf = chrb (13) & chrb (10) 'binary carriage return
Startpos = instrb (data, divider) + lenb (bncrlf) 'Start position
Endpos = branch B (startpos, Data, divider)-lenb (bncrlf) 'end position
Part1 = midb (data, startpos, endpos-startpos) 'content between two delimiters
Firstline = midb (Part1, 1, partition B (Part1, bncrlf)-1 )'
If (partition B (firstline, filename) = 0) then' is a text box, get the text box string content
Stemp = midb (Part1, Region B (Part1, bncrlf & bncrlf) + lenb (bncrlf & bncrlf), lenb (Part1)-Region B (Part1, bncrlf & bncrlf) + lenb (bncrlf & bncrlf ))
Getdata = btos (stemp)
Else' if it is a file, get the binary content of the file
Getdata = midb (Part1, partition B (Part1, bncrlf & bncrlf) + lenb (bncrlf & bncrlf), lenb (Part1)
-Branch B (Part1, bncrlf & bncrlf) + lenb (bncrlf & bncrlf ))
End if
Final = endpos
End Function
You can directly call this function in a program to obtain the required file (or text box) content, as shown below:
<%
C (data, divider, position)
%>
3) Get the file name
As analyzed earlier, the "filename =" Field of the uploaded file data stream contains the file name and absolute path. Generally, we only need to extract the file name in the path. The following is the program code:
<%
Namepos = faster Rev (B2s (firstline), CHR (92) 'firstline is the data in the preceding description, CHR (92)
Indicates "/"
Filename = midb (firstline, namepos + 1, lenb (firstline)-namepos-1) 'get the file name
%>
Use ASP to directly upload files
Traditional ASP programmers can only use filesystemobjectobjects to move, copy, and delete the parent file (.txt). To process binary objects, they have to use the methods described earlier in this article. However, currently, the ADO. Stream object in ASP can be operated simultaneously. Composition This object and binary object (which can be downloaded at http://www.microsoft.com/data) can be used to directly implement the file upload function in ASP. Next, we will introduce the implementation process.
1) Open Stream Object
For a sream object, to save a file, you must save all the content of the object. Therefore, we must create two (or more) stream objects, one of which is the source data stream, that is, to receive the initial binary data; the other is the destination data stream, that is, it receives the data processed by the source data stream and saves it as the desired file.
<%
Set STR = server. Createobject ("ADODB. Stream") 'str is the source data stream
Str. mode = 3' set the open mode. 3 is readable and writable.
Str. type = 1' sets the data type. 1 is binary data.
Str. Open
Set DESC = server. Createobject ("ADODB. Stream") 'desc' is the target data stream.
Desc. mode = 3
Desc. type = 1
Desc. Open
%>
2) Copying between Steam objects
In this section, you must locate the start position of the file in the source data stream and find the length of the file content to copy the file to the destination data stream correctly and save the file. The program code is as follows:
<%
Formdata = request. binaryread (request. totalbytes) 'formdata indicates all the uploaded content.
Str. Write formdata' value: source data stream
Str. Position = count-lenb (result)-2 'position indicates the start position of the file.
Str. copyto DESC, lenb (filecotent) 'lenb (filecontent) indicates the object Length
Desc. savetofile fullpath, 2' save the file with the path and name specified by fullpath
%>
3) Close the steam object
After programming, close and release the steam object, as shown below:
<%
Desc. Close
Set DESC = nothing
Str. Close
Set STR = nothing
%>

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.