Using ASP technology to realize file direct uploading function

Source: Internet
Author: User
Tags format chr end file size file upload final implement requires
Upload | Upload summary: This paper first compared several common file upload technology, and then through an example of the use of ASP technology to directly implement the file upload specific methods.

Keywords: ASP; file upload; binary data

Introduction

Using web technology to realize B/s (browser/server) structure management system is the development trend of office automation. The management system based on Web technology has been developed rapidly in recent years because of its short development cycle, its independent of user platform, easy to realize interactive application, and rapid and efficient collection, processing and release of information. The ASP technology has become the preferred tool for developing management system because of its high development efficiency, good interactivity and strong security.

Many web-based applications involve file upload operations. Common file Upload technology is: Based on HTTP protocol, VB (or Delphi programming language) Development of file upload components, database technology and so on. These methods typically require programmers to master Web technology, database technology, or CGI or component technology at the same time, which requires a higher level of programming. And this article will introduce the use of ASP technology to directly implement the file upload function only programmers master a single ASP technology can greatly reduce the difficulty of programming.

Comparison of several file uploading techniques

1, based on HTTP protocol

This method requires programmers to use third-party software, such as Delphi, VB, in the application of the HTTP protocol first programming, and then the contents of the file to be uploaded in the format of HTTP protocol packaging, and finally to the Web server to send uploaded request messages, so as to achieve file upload. Because Delphi and VB can not write a complete Web network program, can only write Web small application, therefore, this method is only used in limited-function network applications.

2, based on VB (or Delphi, etc.) the development of file upload components

This method uses VB (or Delphi programming language) to develop ASP server components to achieve specific file upload services. It first uses the ASP form function to upload the file (binary format) from the user end to the server side, and then uses the component which the VB develops, carries on the processing to the binary file, becomes may the normal reading and writing file. This method requires the programmer not only to master the ASP language, but also to use VB and other Third-party language components programming, increase the difficulty of development.

3, based on database technology

This method is similar to the previous method. The difference is in the processing of uploaded binaries. It uses a database to hold the binary files. Both small and large databases provide data types for storing binary data, as long as the data is stored in the appropriate fields in append chunk. Although the method is simple and feasible, but because each upload file size is not the same, therefore, the database space will cause a lot of waste, reduce the data access speed, and make the file can only be accessed in the database environment, causing great inconvenience.

Example analysis

However, using ASP technology to directly implement the file upload function, only programmers master a single ASP technology can,

Greatly reduce the difficulty of programming. Here's an example of how to use this method.

1. File Upload Form

First you need to write a form that provides file upload functionality, as shown in the following procedure:

<form action= "upload.asp" Method=post enctype= "Multipart/form-data"
Uploading files: <input type=file name=file1> <br>
<input type=submit name=upload value= "Upload"
</form>
Where the enctype parameter is used to set the MIME encoding of the form, and the property must be set to "Multipart/form-data" when the file (or the text box is included) is uploaded Upload.asp is the ASP program that the server side receives the binary file stream, which will be introduced in the following article.

2, upload file format analysis

Before processing the file, we need to understand the specific format of the uploaded file, by writing the following simple ASP program to view the binary code:

<%
Filesize=request.totalbytes ' Get the size of the uploaded file
Filedata=request.binaryread (filesize) ' Get binary data for uploaded files
Response.BinaryWrite filedata ' Display binary data on the browser
%>
Analyze the binary code of the uploaded file displayed on the browser, find that the code includes four parts (if you upload multiple files or text boxes at the same time, the code is arranged in the order of the upload, the format is the same), each part of the content is separated by carriage return line symbol:

1 First part (starting sign)
-----------------------------7d329631b04d4
2) Part II (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, and we can get the file type. This information is indispensable for saving files correctly.
  
3) Part III (document content)

That is, the binary content of the file, slightly.

4 part fourth (end sign)

-----------------------------7d329631b04d4

Combining the contents of the first and fourth parts, "-----------------------------7d329631b04d4" (each upload, the value is different) played a role in the separator, which marks a piece of data (when there are multiple upload content) Start and end. From the information required to save the file, we first need to get the file name from the "filename" of the second part of the data, and then we need to locate the file correctly, and then use the ASP technology to save the binary file with the original file name. If you upload multiple content at the same time (such as multiple text boxes and files), but also in the same way, each part of the content is included in the separator, but the text box and file forms slightly different, this can be analyzed by a specific binary code to understand.
3, using ASP technology to achieve file storage

Handling of uploading file code

1) Get the separator code

From the above analysis, we already know that a separator plays an important role in segmenting multiple data segments, including text boxes and various types of files. As previously analyzed, the separator appears before the first carriage return line symbol. Therefore, you can get the separator code by following this procedure:

<%
NEWLINE=CHRB & ChrB (a) ' newline represents binary carriage returns
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 a separator
%>
Note: Because the binary byte code is processed here, all functions are using its binary version and adding "B".

2 Get the contents of the file (or text box)

(1) Preliminary function (convert binary string to string)

Upload the contents of the file does not need to go through the binary to the string conversion process, the direct save can be. However, if you need to extract the contents of a text box or the name of a file, you must convert it. Therefore, it is necessary to write a generic and suitable conversion function for Chinese characters. Here's the function code:

Function Btos (BSTR)
If is Null (BSTR) Then
For i = 0 to LenB (BSTR)-1
BCHR = MidB (bstr,i+1,1)
If ASCB (BCHR) >127 Then ' kanji is double byte and is processed together with two characters
temp = TEMP&AMP;CHR (AscW (MidB (BSTR, I+2, 1) &bchr))
i = i+1
Else
temp = TEMP&AMP;CHR (ASCB (BCHR))
End If
Next
End If
Btos = Temp
End Function
(2) Get the contents of the file (or text box)

In an actual Web application, the upload operation may involve multiple content, such as multiple text boxes, multiple files, and so on. Files and text boxes are well differentiated, and the file contains "filename=" strings in its data. Therefore, we write a common function that can be used both to extract the contents of a file and to extract the contents of a text box (binary conversion is required):

Function GetData (ByVal Data, ByVal Divider, Final) ' data represents a binary string; divider represents a delimiter; final indicates the end position of the data
FILENAME=CHRB (102) &AMP;CHRB &AMP;CHRB (108) &AMP;CHRB (a) &AMP;CHRB (a) &AMP;CHRB () &AMP;CHRB (109) &AMP;CHRB (&AMP;CHRB) &AMP;CHRB (34) ' string "filename" binary representation
BNCRLF=CHRB (+) &AMP;CHRB (10) ' binary return character
startpos = INSTRB (data,divider) +lenb (divider) +lenb (BNCRLF) ' Start position
Endpos = INSTRB (startpos,data, divider)-lenb (BNCRLF) ' End position
Part1 = content between MidB (data, startpos, Endpos-startpos) ' two delimiters
Firstline = MidB (part1, 1, INSTRB (part1, Bncrlf)-1) ' content before the description section
if (INSTRB (firstline,filename) =0) Then ' If text box, get text box string contents
Stemp=midb (PART1,INSTRB (PART1,BNCRLF&AMP;BNCRLF) +lenb (BNCRLF&AMP;BNCRLF), LenB (part1)-INSTRB (part1,bncrlf& BNCRLF) +lenb (BNCRLF&AMP;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&AMP;BNCRLF), LenB (part1)
-INSTRB (PART1,BNCRLF&AMP;BNCRLF) +lenb (BNCRLF&AMP;BNCRLF))
End If
Final=endpos
End Function
Call the function directly in your program



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.