用Delphi 6开发ASP上传组件详解
用Delphi 6开发ASP上传组件详解
首先我们来看一个html文件源码,文件名是test.htm,功能是提供用户上传的界面:
<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>
formsize=request.totalbytes '获得表单原始信息的长度
formdata=request.binaryread(formsize) '读取表单原始信息
response.binarywrite formdata'返回表单原始信息
%>
Content-Disposition: form-data; 说明这是表单中的域。
name="mefile"; 域的名称。
filename="C:\Documents and Settings\aaa\My Documents\My Pictures\zzjh.jpg" 上传文件在本地硬盘上的名称。
Content-Type: image/pjpeg 文件类型。
后面是文件本身的数据。
在myobj下的名为Iupfile的Interface下,添加5个属性和一个方法。如果不懂得如何操作,请参见Delphi参考书的相关部分。按F12可以看到生成的相应的myobj_tlb.pas文件,其中的Iupfile接口应该是这个样子:
['{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;
除了完成Iupfile接口中的属性和方法之后,还需要补充一些东西,以便完成我们的任务。最终的Tupfile类的声明如下:
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;
var
AOleVariant : OleVariant;
tmpvar : OleVariant;
contentlength : integer;
i,DeliCount,pos1,pos2,lastpos : integer;
FDelimeter : string;
begin
inherited OnStartPage(AScriptingContext);
FFormInfo := TStringList.Create;
AOleVariant := contentlength;
tmpvar := Request.BinaryRead(AOleVariant);
for i := 1 to contentlength -1 do
begin
FContentData := FContentData + chr(byte(tmpvar[i]));
end;
FDelimeter := copy(FContentData,1,pos1+1);
DeliCount := length(FDelimeter);
lastpos := 1;
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;
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);
if pos1 <> 0 then
begin
isFile := true;
pos1 := pos1 + 10;
pos2 := instr('"',content,pos1);
FFilename := copy(content,pos1,pos2-pos1);
end;
FormValue := copy(content,pos1,length(content)-pos1);
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;
begin
Result := FFormInfo.Values[Formname];
end;
begin
Result := ExtractFileName(FFileName);
end;
begin
Result := length(FFileData);
end;
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;
var
fsout:TFileStream;
begin
fsout := TFileStream.Create(Filename,fmcreate);
try
fsout.Write(Byte(FFileData[1]),Length(FFileData))
finally
fsout.Free;
end;
在命令行下,输入“regsvr32 myobj.dll”。弹出一个对话框,告诉你组件已经注册。如果找不到regsvr32.exe这个文件,它在windows\system32或winnt\system32目录下。
将本文开头提到的test.asp文件修改为如下内容:
Set upfile = Server.CreateObject("myobj.upfile")
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.filetype&"<br>"
%>