<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 at the same time), its attribute must be set to multipart/form-data; upload.asp is the server-side binary file received. The ASP program for stream processing 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
%>
Analyzing the binary code of the uploaded file displayed on the browser, we found 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 entered using Separated by newline characters:
1) The first part (starting flag)
--------------------------7d329631b04d4
2) Part 2 (Document Description)
Content-Disposition: form-data; name=file1; filename=C:/Documents and Settings/Administrator/My Documents/Invitation.doc Content-Type: application/msword
Here, 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) Part 3 (Document Contents)
That is, the binary content of the file, omitted.
4) Part 4 (end mark)
--------------------------7d329631b04d4
Combining the contents of the first and fourth parts, -----------------------------7d329631b04d4 (the value is different every time it is uploaded) Same) plays the role of a separator, which marks the beginning and end of a piece of data (when there are multiple uploaded contents). 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. . 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
Processing of uploaded file code
1) Get the delimiter code
From the above analysis, we already know that separators play 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 we are dealing with binary bytecode here, all functions use its binary version, with b added.
2) Get the file (or text box) content
(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) Get 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 the binary string; divider represents the separator; 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 representation of string filename=
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
By calling this function directly in the program, you can obtain the content of the required file (or text box), as shown below:
<%
Content=getdata (data, divider, position)
%>
3) Get the file name
As has been analyzed before, the filename= field of the upload 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 file upload function
Traditional ASP programmers can only use the FILESYSTEMOBJECT object to move, copy, delete and other operations on text files (.txt). If they need to process binary objects, they have to use the methods introduced earlier in this article. 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 SREAM objects, 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 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 ' Assignment 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
Once programming is complete, the STEAM object should be closed and released as follows:
<%
Desc. Close
Set desc=nothing
Str. Close
Set STR=nothing
%>
Summarize
This article gives 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.