Using ASP to realize file direct upload function

Source: Internet
Author: User
Tags file size file upload implement requires
Upload 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".


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.