one. Analysis of the implementation principle of file upload based on ASP
The basic principle is: use the BinaryRead method of the ADO Stream object to read out all data in the FORM, intercept the required file data, and save it in binary files.
Here is an example of the upload file page (upload.htm):
<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, so that the original data read by using the BinaryRead method in Upload.asp is not only the data of the selected file itself, but also contains descriptions of the path, type, form domain name of the submission page and other related information on the user's hard disk. In this way, we need to extract the specific content of the file from it. According to the analysis, the dividing line between the header information of the data and the data is two pairs of carriage return line breaks, and there is also separating information at the end. We can use a similar 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 The length of the carriage return line break character DivStr=LeftB(FormData,InStrB(FormData,CLStr)-1)DataSize=InStrB(DataStart+1,FormData,DivStr)-DataStart-2FormData=MidB(FormData,DataStart,DataSize)FormData is the content of the file.
The corresponding treatment can be performed as needed in the middle. The final job is to save the file. There are two ways to save: one is to use binary file operation methods in programs such as VB or VC, add appropriate type libraries to the project, and finally compile it into a DLL file, and 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 piece of code is a related operation for 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,InStrB(FormData,CLStr)-1) DataSize=InStrB(DataStart+1,FormData,DivStr)-DataStart-2 FormData=MidB(FormData,DataStart,DataSize) 'Create a binary file and write FormData to Open Pathname For Binary As 1 Put #1,,FormData Close #1 SaveFile="OK!" End FunctionThe second method is to use the binary file operation method provided in ADO STREAM to complete it. The statement to save the file is: StreamOBJ.SaveToFile (fileName,2). In this kind of operation, we can store the relevant operations in a class file. When applying, just include the class file in the ASP program. For specific processing methods, please refer to the relevant introduction.