程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 如何用Delphi 6開發ASP上傳組件詳解

如何用Delphi 6開發ASP上傳組件詳解

編輯:Delphi

文件上傳是web開發中經常要用到的功能,但ASP本身和內置的組件都不支持文件上傳功能。網上流傳的一些第三方組件雖然能夠解決這個問題,但大多是要收費的,更別說Open Source了。本文將詳細剖析WEB文件上傳的原理,以及一步步指導讀者如何用Delphi6開發一個ASP上傳組件。

  1 Html文件分析

  首先我們來看一個Html文件源碼,文件名是test.htm,功能是提供用戶上傳的界面:

<Html>
<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="這個是這個">
   <input type=text name=a6 value="fdsaf">
   <input type=submit name=ok value="OK">
   </form>
</center>
</body>
</Html>

  這個文件裡包含了一個名為mainForm的form,以及隨手寫的一些input域。注意這個form和一般的form有兩個不同的地方:一是它有一個type=file的域,沒有value。用浏覽器打開這個文件時,這個域會表現為一個右側有“浏覽”字樣的文件輸入框,用戶可以通過它來選擇本地硬盤上的文件。二是form有一個特殊的屬性:enctype="multipart/form-data"。這個屬性告訴浏覽器要上傳二進制文件,並進行相應編碼。

這種編碼會產生什麼樣的表單信息呢?讓我們來看看test.asp,也就是接受表單的ASP文件的源碼,它非常簡單:

<%
formsize=request.totalbytes   '獲得表單原始信息的長度
formdata=request.binaryread(formsize)   '讀取表單原始信息
response.binarywrite formdata  '返回表單原始信息
%>
如讀者在注釋中了解的,這段代碼的功能是將表單的原始信息返回。讓我們來看看它的運行效果。將
這兩個文件置於web目錄下,訪問test.htm。在文件輸入框中,選擇一個文件(我選了一個jpg圖片,
不過最大不要太大)。提交,然後可以看到這樣一堆亂七八糟的信息:
-----------------------------7d2227629012e Content-Disposition: form-data;
name="mefile"; filename="C:Documents and SettingsaaaMy DocumentsMy
Pictureszzjh.jpg" Content-Type: image/pjpeg (作者注:以下為亂碼)
-----------------------------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" 這個是這個
-----------------------------7d2227629012e Content-Disposition: form-data;
name="a6" fdsaf -----------------------------7d2227629012e Content-Disposition:
form-data; name="ok" OK -----------------------------7d2227629012e--
這就是用"multipart/form-data"方式編碼的表單原始信息。其中那一段看起來是亂碼的部分,就是jpg圖片的編碼。
分析一下這段信息的格式:  
-----------------------------7d2227629012e 這是各個域之間的分隔符。
Content-Disposition: form-data; 說明這是表單中的域。
name="mefile"; 域的名稱。
filename="C:Documents and SettingsaaaMy DocumentsMy Pictureszzjh.jpg" 上
傳文件在本地硬盤上的名稱。
Content-Type: image/pjpeg 文件類型。
後面是文件本身的數據。


其它各個域的信息也可以以此類推。

  眾所周知,在ASP中,使用request對象,可以訪問用戶提交表單的各個域。因為request對象

  會對原始的表單信息進行解析,提取出表單中每個域的值。但是,request並不能解析這

  "multipart/form-data"格式的表單信息。這就是ASP不能直接支持文件上傳的原因所在。讀者可以

  試試,在test.ASP中,用request("mefile")這樣的格式,是不能讀取到正確的信息的。

  問題的症結已經找到,解決的思路也很簡單:用Delphi開發一個COM組件,接受這種原始表單信息,

  將各個域一一提取出來,返回給ASP文件。也就是完成request對象沒有完成的功能。

  2 用Delphi開發組件

  Delphi6對開發ASP組件提供了極好的支持,大大簡化了我們的開發過程。

  啟動Delphi 6,選擇File-New-Other-ActiveX-ActiveX Library,這樣就建立了一個ActiveX庫。將此Library改名為myobj,存盤。選擇File-New-Other-ActiveX-Active Server Object,在CoClassname中填入upfile,確定。這時會跳出一個標題為myobj.tlb的對話框,這是Delphi特有的以可視化方式編輯COM接口的功能,用Delphi開發過COM的讀者應該比較熟悉。

  在myobj下的名為Iupfile的Interface下,添加5個屬性和一個方法。如果不懂得如何操作,

  請參見Delphi參考書的相關部分。按F12可以看到生成的相應的myobj_tlb.pas文件,其中的

  Iupfile接口應該是這個樣子:

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;

其中的OnStartPage方法和OnEndPage方法是Delphi默認生成的,其它的是手動加入的。

  切換到unit1.pas(也是Delphi自動生成的),改名為upfile.pas存盤。可以看到存在一個Tupfile類的聲明,它是繼承自TASPObject類和Iupfile接口的。Delphi 6已經自動生成了相應的代碼。接下來的任務就是實現這個接口。

  除了完成Iupfile接口中的屬性和方法之後,還需要補充一些東西,以便完成我們的任務。最終的

  Tupfile類的聲明如下:

Tupfile = class(TASPObject, Iupfile)
 public
 protected
  procedure OnEndPage; safecall; //頁面開始
  procedure OnStartPage(const AScriptingContext: IUnknown); safecall; //頁面
結束
  procedure FileSaveAs(Filename: OleVariant); safecall; //保存文件
  function Get_Form(Formname: OleVariant): OleVariant; safecall; //
  function Get_FileName: OleVariant; safecall; 
  function Get_FileSize: Integer; safecall;
  function Get_FileData: OleVariant; safecall;
  function Get_FileType: OleVariant; safecall;
 private
  FContentData:string;
  FFileData,FFileName,FFileType:string;
  FFormInfo:TStringList;
  function instr(str1,str2:string;startpos:integer):integer;
  procedure AnalyFormData(content:string);
 end;

  下面我們來一一分析這些成員的具體實現。

procedure Tupfile.OnStartPage(const AScriptingContext: IUnknown);
var
 AOleVariant : OleVariant;
 tmpvar : OleVariant;
 contentlength : integer;
 i,DeliCount,pos1,pos2,lastpos : integer;
 FDelimeter : string;
begin
 inherited OnStartPage(AScriptingContext);
 FFormInfo := TStringList.Create;
 contentlength := Request.TotalBytes;
 AOleVariant := contentlength;
 tmpvar := Request.BinaryRead(AOleVariant);
 for i := 1 to contentlength -1 do
 begin
  FContentData := FContentData + chr(byte(tmpvar[i]));
 end;
 pos1 := pos(#13#10,FContentData);
 FDelimeter := copy(FContentData,1,pos1+1);
 DeliCount := length(FDelimeter);
 lastpos := 1;
 pos1:=0;
 while pos2>=pos1 do
 begin
  pos1 := instr(FDelimeter,FContentData,lastpos);
  if pos1 = 0 then Break;
  pos1 := pos1 + DeliCount;
  pos2 := instr(FDelimeter,FContentData,pos1)-1;
  AnalyFormData(copy(FContentData,pos1,pos2-pos1-1));
  lastpos := pos2;
 end;
end;

前面說過,OnStartPage方法是Delphi自動生成的,在裝載頁面時發生。在這個方法中,我們完成一些初始化的任務:讀取表單的原始數據,解析表單中的域,並存入相應的屬性中,以備調用。

  由於Delphi已經對ASP中的對象進行了很好的封裝,所以即使在Delphi環境下,也可以方便地調用它,就象在ASP中一樣,例如Request.TotalBytes。首先將原始表單數據讀入到一個OleViarians類型的pvar中,然後通過一個循環,將它轉換為Delphi中的string格式,並存放在FContentData中。

  接下來,通過查找換行符,解析出分隔符的內容和長度。然後在一個循環中,用AnalyFormData成員函數一一解析出每個域。初始化工作就這樣完成了。

  再看AnalyFormData函數的實現:

procedure Tupfile.AnalyFormData(content: string);
var
 pos1,pos2:integer;
 FormName,FormValue:string;
 isFile:boolean;
begin
 isFile := false;
 pos1 := instr('name="',content,1)+6;
 pos2 := instr('"',content,pos1);
 FormName := copy(content,pos1,pos2-pos1);
 //檢查是否文件
 pos1 := instr('filename="',content,pos2+1);
 if pos1 <> 0 then
 begin
  isFile := true;
  pos1 := pos1 + 10;
  pos2 := instr('"',content,pos1);
  FFilename := copy(content,pos1,pos2-pos1);
 end;
 pos1 := instr(#13#10#13#10,content,pos2+1)+4;
 FormValue := copy(content,pos1,length(content)-pos1);
 if isfile then
 begin
  FFileData := FormValue;
  //查找文件類型信息
  pos2 := instr('Content-Type: ',content,pos2+1);
  if pos2 <> 0 then
  begin
   pos2 := pos2 + 14;
   FFileType := copy(content,pos2,pos1-4-pos2);
  end;
 end
 else
 begin
 FFormInfo.add(FormName+'='+FormValue);
 end;
end;

如注釋中所表達的,AnalyFormData提取原始數據中的域。如果是域是文件類型,則將文件類型和文件數據分別放入FFileType和FFileData中。如果是其它類型,則將名稱和值放入一個TStringlist類型的FFormInfo中。FFormInfo中維護著除文件類型外的所有域的信息,以“名稱=值”的格式存放。

function Tupfile.Get_Form(Formname: OleVariant): OleVariant;
begin
  Result := FFormInfo.Values[Formname];
end;

  這個函數返回域的值。只需要簡單地調用FFormInfo的values方法,就可以得到相應的值。這是在Tstringlist類內部實現的。

function Tupfile.Get_FileName: OleVariant;
begin
 Result := ExtractFileName(FFileName);
end;
function Tupfile.Get_FileSize: Integer;
begin
 Result := length(FFileData);
end;
function Tupfile.Get_FileData: OleVariant;
var
 i:integer;
begin
 Result := VarArrayCreate( [0,length(FFileData)], varByte );
 for i := 0 to length(FFileData)-1 do
 begin
  Result[i] := Byte(FFileData[i+1]);
 end;
end;

  這三個函數分別返回文件的名稱、大小、數據。要注意的是,在返回文件數據時,必須進行相應的轉換,將Delphi中的string類型轉換為OleVariant類型。

procedure Tupfile.FileSaveAs(Filename: OleVariant);
var
 fsout:TFileStream;
 i:integer;
 afile:file of byte;
begin
 fsout := TFileStream.Create(Filename,fmcreate);
 for i := 1 to length(FFileData) do
 begin
  fsout.Write(Byte(FFileData[i]),1)
 end;
 fsout.Free;
end;

這個方法將文件保存到服務器上的磁盤。

  編譯myobj這個project,得到一個myobj.dll文件。開發工作就此完成。

  3 使用ASP上傳組件

  在命令行下,輸入“regsvr32 myobj.dll”。彈出一個對話框,告訴你組件已經注冊。如果找不到regsvr32.exe這個文件,它在Windowssystem32或winntsystem32目錄下。

  將本文開頭提到的test.ASP文件修改為如下內容:

<%'建立對象
Set upfile = Server.CreateObject("myobj.upfile")
'獲得表單對象
response.write upfile.form("a1")&"<br>"
response.write upfile.form("a2")&"<br>"
response.write upfile.form("a3")&"<br>"
response.write upfile.form("a4")&"<br>"
response.write upfile.form("a5")&"<br>"
response.write upfile.form("a6")&"<br>"
'獲得文件大小
response.write "文件字節數:"&upfile.filesize&"<br>"
'獲得文件類型
response.write "文件類型:"&upfile.filetype&"<br>"
'獲得文件名,保存文件
upfile.filesaveas(Server.MapPath("")+upfile.filename)
set upfile = nothing
%>

  再次訪問test.htm,提交表單。現在你可以看到相關的返回信息,並且在服務器上test.ASP所處

  的目錄下找到上傳的文件。

  這個組件只能上傳單個文件,但根據同樣的原理,一次上傳多個文件的功能也是不難實現的。有興趣的讀者可以自行嘗試。







  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved