ASP implementation of File Upload method _ Application Skills

Source: Internet
Author: User
Tags file upload

A Implementation principle analysis of file uploading based on ASP
The basic principle is: Use ADO Stream object's BinaryRead method to read out all the data in form, intercept the required file data and save it in binary file.
Here is an example of uploading a file page (upload.htm):

  
    
      
     
   

The file object is used in the program, so the raw data that is read by the BinaryRead method in the upload.asp is not only the data of the selected file itself, but also the description of the path, type, the form domain name of the file on the user's hard disk, and the related information. So we need to extract the specific contents of the file. According to the analysis, the data's header information and the data dividing line is two pairs of carriage return line character, the tail also has the separation information, we may use similar method to obtain the file data.

Dim formdata.formsize,datastart,clstr,divstr
formsize=request.totalbytes
formdata=request.binaryread ( formsize)
CLSTR=CHRB (&CHRB)
datastart=instrb (formdata.clstr&clstr) +4
' 4 is the length of a carriage return line feed
Divstr=leftb (FORMDATA,INSTRB (FORMDATA,CLSTR)-1)
DATASIZE=INSTRB (DATASTART+1,FORMDATA,DIVSTR)-datastart-2
formdata=midb (formdata,datastart,datasize)
Formdata is the content of the document.

In the middle according to the need, may carry on the corresponding processing. The last job is to save the file. There are two ways to save: One is the use of VB or VC programs such as binary file operation method, in the project to add the appropriate type library, and eventually compiled into a DLL file, use the DLL file to register it. File storage procedures are as follows:

 public Function SaveFile (Pathname As String) As String Dim Objcontext as Objectco ntext Dim objrequest as Request set Objcontext=getobjectcontext () Set Objrequest=objcontext ("Request") ' to The next piece of code is the operation of file storage. Dim FormData () As Byte,clstr,divstr Dim datastart As long,datasize as Long datasize=objrequ Est. TotalBytes Redim FormData (DataSize-1) formdata=objrequest.binaryread (datasize) CLSTR=CHRB (a) & ChrB (10 ) DATASTART=INSTRB (Formdata,clstr & Clstr) +4 Divstr=leftb (FORMDATA,INSTRB (FORMDATA,CLSTR)-1) DataSize=I NSTRB (DATASTART+1,FORMDATA,DIVSTR)-datastart-2 formdata=midb (formdata,datastart,datasize) ' Creates a binary file and writes FormData to
   Where the Open Pathname for Binary as 1 is put #1, FormData close #1 savefile= "ok!" End Function 

The second method is done using the binary file operation method provided in the ADO stream, and the statement to save the file is: Streamobj.savetofile (filename,2). In this operation, we can store the relevant operations in a class file, in the application, directly to the class file included in the ASP program can be. For specific processing, see the introduction.
two. Example of file Upload implementation method
implementation of File upload can use components or no components. For component classes, such as Microsoft Posting Acceptor (MPA), a free server component released by Microsoft, this type of component is also easier to install. For the MPA of Microsoft, it is OK to run its installation files. And the general DLL component form, we need to register. For example, to use AspcnUP.dll, just execute regsvr32 [path\] on Window 2000 AspcnUP.dll, the system will be able to use the component if it is prompted for a successful registration; For a component-free class, when used, it is OK to include the following statement in the handler:

!--#include file= "Upload.inc"-->
set upload=new upload_5xsoft ' Set up the upload object

For the properties and how to do this, see the use manual for that component.
The following is an example of a aspcnUP.dll component that uploads some type of file source code (upload.asp):

<% @ 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
%>

Browser/server application mode is also rapidly developing, in Microsoft's new asp.net, has built-in file upload function, the use is very simple and convenient. As a new technology, ASP. NET is not just an ASP's simple upgrade, it is a new framework for Web development, which contains many new features. Asp. NET provides easier to write, more structured code, which makes it easier to reuse and share, and to develop more practical programs.

Related Article

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.