When we are working on web projects, we often need to use the function of uploading files, but uploading various types of files in a browser/server-based application environment has always troubled users. So what is the method of ASP to upload files?
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.
two. File upload implementation method example
To implement file uploads, you can use components or components-free methods. For component classes, such as Microsoft posting acceptor (mpa for short), it is a free server component released by Microsoft. The installation of such components is also more convenient. For Microsoft's mpa, just run its installation files. For general DLL component form, we need to register. For example, to use aspcnUP.dll, just execute regsvr32 [path/]aspcnUP.dll on Window 2000, and the system prompts that the registration is successful, you can use this component; for the componentless class, when using it, just include the following statements in the handler:
<!--#include FILE="upload.inc"-->set upload=new upload_5xSoft 'Create upload object
For properties and operation methods, please refer to the user manual of this component.
The following is the source code (upload.asp) for uploading some types of files using the aspcnUP.dll component as an example:
<% @ 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%>The browser/server application model is still developing rapidly. In Microsoft's newly launched ASP.NET, the file upload function has been built-in, making it very simple and convenient to use. As a brand new technology, ASP.NET is not just a simple upgrade of ASP. It is a brand new framework for web development, which contains many new features. ASP.NET provides easier to write and clearer structure code, and we will be able to reuse and share more easily, thus developing more practical programs.
The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support the wrong new technology channel!