Using WEB technology to implement a management system with a B/S (browser/server) structure is the development trend of office automation. Management systems based on WEB technology have developed rapidly in recent years because they have a short development cycle; are independent of user platforms; are easy to implement interactive applications; and can collect, process and publish information quickly and efficiently. ASP technology has gradually become the preferred tool for developing management systems due to its high development efficiency, good interactivity, and strong security.
Many WEB-based applications involve file upload operations. Common file upload technologies include: based on HTTP protocol; based on file upload components developed by VB (or DELPHI and other programming languages); based on database technology, etc. These methods generally require programmers to master WEB technology, database technology or CGI technology or component technology at the same time, and have higher requirements for programmers. The use of ASP technology to directly implement the file upload function that this article will introduce only requires programmers to master a single ASP technology, which greatly reduces the difficulty of programming.
Comparison of several file upload technologies
1. Based on HTTP protocol
This method requires programmers to use third-party software, such as DELPHI, VB, etc., to first perform HTTP protocol programming in the application, and then package the content of the file to be uploaded in the format of the HTTP protocol , and finally sends an upload request message to the WEB server to achieve file upload. Because DELPHI and VB cannot write complete WEB network programs and can only write small WEB applications, this method is only used for network applications with limited functions.
2. File upload component developed based on VB (or DELPHI, etc.)
This method uses VB (or DELPHI, etc. programming language) to develop ASP server components to implement specific file upload services. It first uses the ASP form function to upload files (binary format) from the client to the server, and then uses VB-developed components to process the binary files into files that can be read and written normally. This method requires programmers not only to master the ASP language, but also to use third-party languages such as VB for component programming, which increases the difficulty of development.
3.
This method is similar to the previous method based on database technology. 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. Just store the data in the corresponding fields in the Append Chunk method. Although this method is simple and feasible, because the size of the file uploaded each time is different, it will cause a great waste of database space and reduce the data access speed; and the file can only be uploaded in the database environment. access, causing great inconvenience.
Example Analysis
However, using ASP technology to directly implement the file upload function only requires the programmer to master a single ASP technology, which
greatly reduces the difficulty of programming. Below we will introduce how to use this method through an example.
1. File upload form
First you need to write a form that can provide 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>
Among them, the enctype parameter is used to set the MIME encoding method of the form. When uploading a file (or containing a text box), its attribute must be set to "multipart/form-data"; upload.asp is the server side The ASP program that processes the received binary file stream will be introduced later in this article.
2. Upload file format analysis
Before processing the file, we must first understand the specific format of the uploaded file. We can view its binary code by writing the following simple ASP program:
<%
filesize=Request.TotalBytes 'Get the size of the uploaded file
filedata=Request.BinaryRead(filesize) 'Get the binary data of the uploaded file
Response.BinaryWrite filedata 'Display binary data on 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 codes are arranged in the order of uploading, with the same format), and the content of each part is Separated by carriage return and line feed symbols:
1) The first part (starting mark)
--------------------------7d329631b04d4
2) Part 2 (File Description)
Content-Disposition: form-data; name="file1"; filename="C:Documents and SettingsAdministratorMy DocumentsInvitation.doc" Content-Type: application/msword
in Therefore, we can get the file name and absolute path of the uploaded file, as well as the file type. This information is essential to save the file correctly.
3) The third part (file content)
is the binary content of the file, omitted.
4) Part 4 (End Mark)
--------------------------7d329631b04d4
Let’s look at the contents of Part 1 and Part 4 , "--------------------------------7d329631b04d4" (the value is different every time you upload it) plays the role of a separator, It marks the beginning and end of a piece of data (when there are multiple uploads). In terms of the information needed to save the file, we first need to obtain the file name from the "filename" of the second part of the data, then we need to correctly locate the starting position of the file, and finally use ASP technology to save the binary file with its original file name. That’s it. If multiple contents (such as multiple text boxes and files) are uploaded at the same time, they are processed in the same way. Each part of the content is included in the delimiter, but the text boxes and files are expressed in slightly different ways. This can be done by Specifically analyze its binary code to understand.
3. Use ASP technology to implement file storage
and upload file code processing
1) Obtain the delimiter code.
From the above analysis, we already know that the delimiter plays an important role in dividing multiple data segments (including text boxes and various types of files) . As has been analyzed before, the separator appears before the first carriage return and line feed symbol. Therefore, the delimiter code can be obtained through the following program:
<%
newline=chrB(13) & chrB(10) 'newline represents the binary carriage return character
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(instrb(filedata,newline))-1) 'divider is the divider
%>
Note: Because all processes here are binary bytecodes, all functions use their binary versions with "b" added.
2) Obtain the content of the file (or text box)
(1) Preparatory function (convert binary string into string)
The content of the uploaded file does not need to go through the conversion process from binary to string, and can be saved directly. However, if you need to extract the contents of the text box or the name of the file, you must perform conversion. Therefore, it is necessary to write a universal conversion function suitable for Chinese characters. The following is the function code:
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 'Chinese characters are double bytes, so two characters must be processed together
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) Obtain the content of the file (or text box).
In actual WEB applications, the upload operation may involve multiple contents, such as multiple text boxes, multiple files, etc. Files and text boxes are easily distinguished. The file data contains the "filename=" string. Therefore, we wrote the following general function, which can be used to extract both file content and text box content (binary conversion is required):
Function getdata(byval data, byval divider, final) 'data represents a binary string; divider represents division symbol; final represents 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) 'Binary of string "filename" express
bncrlf=chrb(13)&chrb(10) 'Binary carriage return character
startpos = instrb(data,divider)+lenb(divider)+lenb(bncrlf) 'Start position
endpos = instrb(startpos,data, divider)-lenb(bncrlf) 'End position
part1 = midb(data, startpos, endpos-startpos) 'Content between two separators
firstline = midb(part1, 1, instrb(part1, bncrlf)-1) ' Description paragraph before the content
If (instrb(firstline,filename)=0) Then 'If it is a text box, get the text box string content
stemp=midb(part1,instrb(part1,bncrlf&bncrlf)+lenb(bncrlf&bncrlf),lenb(part1)-instrb(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, instrb (part1, bncrlf&bncrlf)+lenb (bncrlf&bncrlf), lenb (part1)
-instrb(part1,bncrlf&bncrlf)+lenb(bncrlf&bncrlf))
End If
Final=endpos
End function
Call this function directly in the program to obtain the content of the required file (or text box), as shown below:
<%
Content=getdata (data, divider, position)
%>
3) Obtaining the file name has been analyzed before. The "filename=" field of the uploaded file data stream contains the name and absolute path of the file. Generally speaking, we only need to extract the file name in the path. The following is the program code:
<%
namepos=instrrev(B2S(firstline),chr(92)) 'firstline is the description part data obtained above, chr(92)
express"/"
filename=midb(firstline,namepos+1,lenb(firstline)-namepos-1) 'Get the file name
%>
Use ASP to directly implement the file upload function.
Traditional ASP programmers can only use the FILESYSTEMOBJECT object to move, copy, delete and other operations on text files (.txt). If you need to process binary objects, you have to use those introduced earlier in this article. method to achieve. However, now the ADO.STREAM object in ASP can operate text objects and binary objects at the same time (can be downloaded at http://www.microsoft.com/data ), and you can use it to directly implement the file upload function in ASP. Below, we introduce its implementation process.
1) Open the STREAM object.
For the SREAM object, if you want to save the file, you must save the entire contents of the object. Therefore, we must create two (or more) STREAM objects, one of which is the source data stream, which receives the initial binary data; the other is the destination data stream, which receives the processed data from the source data stream, and Finally save to 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 'Set 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 content between STEAM objects
In this part, you must locate the beginning of the file in the source data stream and find out the length of the file content before you can correctly copy the file to the destination data stream and save the file. The program code is as follows:
<%
formdata=Request.BinaryRead(Request.TotalBytes) 'formdata is all uploaded content
str.Write formdata 'Assign source data stream
str.position=count-lenb(result)-2 'position points to the beginning of the file
str.copyto desc, lenb(filecotent) 'lenb(filecontent) represents the length of the file
desc.SaveToFile fullpath,2 'Save the file with the path and name specified by fullpath
%>
3) Close the STEAM object.
After programming is completed, the STEAM object should be closed and released, as shown below:
<%
Desc. Close
Set desc=nothing
Str. Close
Set STR=nothing
%>
Summary
This article provides a method to directly implement file upload using ASP, which has been well applied in the information management system developed by this unit. Practice has proven that this method is simpler and more efficient than several traditional file upload methods.