Using Delphi 6 to develop ASP upload components detailed

Source: Internet
Author: User
Tags file size file upload integer save file domain
Upload | detailed

File uploads are a feature that is often used in web development, but the ASP itself and the built-in components do not support file upload. Some of the Third-party components circulated on the Internet can solve this problem, but most of them have to be charged, let alone open source. This article will be detailed analysis of the principle of Web file upload, and step-by-Step guide readers how to use Delphi6 to develop an ASP upload components.

1 HTML file Analysis
First we look at an HTML file source code, filename is test.htm, the function is to provide user upload interface:

<body>
<center>
<form name= "mainform" enctype= "Multipart/form-data"
action= "Test.asp" method=post>
<input Type=file name=mefile><br>
<input type=hidden name=a1 value= "Fdsaf" >
<input type=hidden name=a2 value= "Fdsaf" >
<input type=hidden name=a3 value= "Fdsaf" >
<input type=hidden name=a4 value= "Fsdfsdsaf" >
<input Type=hidden name=a5 value= "This Is this" >
<input type=text name=a6 value= "Fdsaf" >
<input type=submit name=ok value= "OK" >
</form>
</center>
</body>

This file contains a form named MainForm, as well as some input fields with handwriting. Notice that this form and the general form have two different places: one is that it has a type=file field and no value. When you open the file in a browser, the field appears as a File entry box with the word "browse" on the right, which allows the user to select files on the local hard drive. The second is that the form has a special attribute: enctype= "Multipart/form-data". This property tells the browser to upload the binary file and encode it accordingly.
What kind of form information does this encoding produce? Let's take a look at test.asp, which is the source of the ASP file that accepts the form, it's very simple:

<%
Formsize=request.totalbytes ' Gets the length of the form's original information
Formdata=request.binaryread (formsize) ' Read form raw information

Response.BinaryWrite formdata ' return form raw information
%>

As the reader knows in the comments, the function of this code is to return the original information of the form. Let's take a look at how it works. Place these two files in the Web directory and access the test.htm. In the File input box, select a file (i chose a JPG image, but not too large). Submit, and then you can see such a mess of information:

-----------------------------7d2227629012e Content-disposition:form-data; Name= "Mefile"; Filename= "C:\Documents and Settings\aaa\my Documents\My Pictures\zzjh.jpg" Content-type:image/pjpeg (author Note: The following is garbled)----- ------------------------7d2227629012e Content-disposition:form-data; Name= "A1" Fdsaf-----------------------------7d2227629012e content-disposition:form-data; Name= "A2" Fdsaf-----------------------------7d2227629012e content-disposition:form-data; Name= "A3" Fdsaf-----------------------------7d2227629012e content-disposition:form-data; Name= "A4" Fsdfsdsaf-----------------------------7d2227629012e content-disposition:form-data; Name= "A5" This is this-----------------------------7d2227629012e content-disposition:form-data; Name= "A6" Fdsaf-----------------------------7d2227629012e content-disposition:form-data; Name= "OK" OK-----------------------------7d2227629012e--

This is the form's original information encoded in "Multipart/form-data" mode. The part that seems to be garbled is the encoding of JPG images. (The actual JPG image encoding may be much longer than this, depending on the file size.) For the sake of convenience, the author retains only a small portion. )
Analyze the format of this information:

-----------------------------7d2227629012e This is the separator between each field.
Content-disposition:form-data; Description This is the field in the form.
Name= "Mefile"; The name of the domain.
Filename= the name of the "C:\Documents and Settings\aaa\my documents\my pictures\zzjh.jpg" upload file on the local hard disk.
Content-type:image/pjpeg file type.
The following is the data for the file itself.

The information in each of the other fields can be, and so on.
It is well known that in ASP, the request object is used to access the individual fields of the user submitting form. Because the Request object resolves the original form information, it extracts the value of each field in the form. However, the request does not resolve the form information in this "multipart/form-data" format. This is why ASPs cannot directly support file uploads. Readers can try, in test.asp, in the format of request ("Mefile"), is not able to read the correct information.
The crux of the problem has been found, the solution is also very simple: the development of a COM component Delphi, accept this raw form information, each domain one by one extracted, returned to the ASP file. That is, the completion of the request object does not complete the function.

2 Development Components with Delphi

DELPHI6 provides excellent support for the development of ASP components, greatly simplifying our development process.
Start Delphi 6, select the File-new-other-activex-activex library, and set up an ActiveX library. Rename this library to MyObj and save it. Select File-new-other-activex-active Server Object and fill in the Upfile in Coclassname, OK. At this time will jump out of a title for Myobj_tlb dialog box, this is the Delphi specific to the visual way to edit the COM interface function, with Delphi developed COM readers should be more familiar.
Under MyObj, under the interface named Iupfile, add 5 properties and one method. If you do not know how to operate, see the relevant parts of Delphi reference books. Press F12 to see the corresponding Myobj_tlb.pas file generated, where the Iupfile interface should look like this:

Iupfile = Interface (IDispatch)
[' {5c40d0eb-5a22-4a1e-8808-62207ae04b51} ']
Procedure OnStartPage (const ascriptingcontext:iunknown); Safecall;
Procedure OnEndPage; Safecall;
function Get_form (formname:olevariant): olevariant; Safecall;
function get_filename:olevariant; Safecall;
function Get_filesize:integer; Safecall;
Procedure FileSaveAs (filename:olevariant); Safecall;
function get_filedata:olevariant; Safecall;
function get_filetype:olevariant; Safecall;
Property Form[formname:olevariant]: olevariant read get_form;
Property Filename:olevariant read Get_filename;
Property Filesize:integer read get_filesize;
Property Filedata:olevariant read Get_filedata;
Property Filetype:olevariant read Get_filetype;
End

The OnStartPage method and the OnEndPage method are the default generation of Delphi, others are joined manually.
Switch to Unit1.pas (also Delphi automatically generated), renamed for Upfile.pas disk. You can see a declaration of a tupfile class that inherits from the Taspobject class and the Iupfile interface. Delphi 6 has automatically generated the appropriate code. The next task is to implement this interface.
In addition to completing the properties and methods in the Iupfile interface, something needs to be added to complete our task. The final Tupfile class declaration is as follows:

Tupfile = Class (Taspobject, Iupfile)
Public
Protected
Procedure OnEndPage; Safecall; Page start
Procedure OnStartPage (const ascriptingcontext:iunknown); Safecall; End of page
Procedure FileSaveAs (filename:olevariant); Safecall; Save File
function Get_form (formname:olevariant): olevariant; Safecall; //
function get_filename:olevariant; Safecall;
function Get_filesize:integer; Safecal



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.