谢谢!收到了,待消化吸收后,再给你送分,ok unit Unit1; //2000-12-13interfaceuses Windows, Messages, SysUtils, Classes, HTTPApp;type TWebModule1 = class(TWebModule) procedure WebModule1WebActionItem1Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); private { Private declarations } public { Public declarations } DataStream:TMemoryStream; //全部 FileStream:TMemoryStream;//文件内容 DjStr:String; function StrSearch(sPos:Integer;keystr:string;sg:Integer):Integer; function Pos_Reverse(var Key:string;SearchLen:integer=-1):integer; Procedure FileToSave_Test(fileName:string); function GetFilePos(var bgPos:Integer):Integer; function GetFileContent():integer; function GetFileNameValue(FieldName:string):string; Function GetFieldValueA(FieldName:string):string; Procedure Initialize(); end;var WebModule1: TWebModule1;implementation{$R *.DFM}Procedure TWebModule1.Initialize(); var i:Integer; Buffer:char; pBuffer:pchar; extBuffer:pchar; ActualReadCount:integer; Totalbytes,Availablebytes:Integer; begin Self.DataStream:=TMemoryStream.Create(); Self.FileStream:=TMemoryStream.Create(); TotalBytes:=Request.ContentLength; AvailableBytes:=Length(Request.Content); pBuffer:=PChar(Request.Content); Self.DataStream.Write(pBuffer^,AvailableBytes); if TotalBytes>AvailableBytes then begin AvailableBytes:=TotalBytes-AvailableBytes; GetMem(ExtBuffer,AvailableBytes); repeat ActualReadCount:=Request.ReadClient(extBuffer^,Availablebytes); Self.DataStream.Write(extBuffer^,ActualReadCount); AvailableBytes:=AvailableBytes-ActualReadCount; until AvailableBytes<1; FreeMem(extBuffer); end; i:=0; Self.DjStr:=''; DataStream.Position:=0; Repeat DataStream.Read(Buffer,1); if Buffer=#13 then break; DjStr:=DjStr+Buffer; inc(i); DataStream.Position:=i; until Buffer=#13; end; function TWebModule1.StrSearch(sPos:Integer;keystr:string;sg:Integer):Integer; var i,k,strLen:Integer; ErrmsgLength:Integer; Buffer:char; begin K:=0; i:=sPos; Self.DataStream.Position:=i; Self.DataStream.Read(Buffer,1); strLen:=Length(KeyStr); ErrMsgLength:=Self.DataStream.Size; while(i<ErrMsgLength) do begin if buffer=KeyStr[K+1] then inc(k) else begin i:=i-k; k:=0; end; inc(i); if k=strLen then begin if sg=0 then result:=i-k else result:=i; exit; end; Self.DataStream.Position:=i; Self.DataStream.Read(Buffer,1); end;//while end; result:=-1; End;function TWebModule1.GetFilePos(var bgPos:Integer):Integer; var i,inted:Integer; begin result:=-1; i:=strsearch(0,'Content-Type:',1); if i<0 then exit; i:=strsearch(i,char($0d)+char($0A)+char($0d)+Char($0a),1); if i<0 then exit; bgPos:=i;//file stratr position inted:=strsearch(i,djstr,0)-3; if i<0 then exit; Result:=Inted-i+1; End; function TWebModule1.GetFileContent():integer; var Buffer:Pchar; bgPos,Len:Integer; begin fileStream.SetSize(0); fileStream.Position:=0; result:=0; Len:=Self.GetFilePos(bgPos); if Len<=bgPos then exit; try GetMem(Buffer,Len); DataStream.Position:=bgPos; DataStream.Read(Buffer^,Len); FileStream.Write(Buffer^,Len); finally FreeMem(Buffer); end; end;Function TWebModule1.GetFileNameValue(FieldName:string):string; var KeyStr:string;i:Integer; // varbyte:byte; Buffer:char; begin Result:=''; KeyStr:=djStr+char($0D)+Char($0A)+ 'Content-Disposition: form-data; name="'+fieldName+'"' ; i:=StrSearch(0,KeyStr,1); if i<0 then exit; i:=StrSearch(i,'filename="',1); if i<0 then exit; DataStream.Position:=i; DataStream.Read(Buffer,1); if Buffer='"' then exit; while(buffer<>'"') do begin Result:=Result+Buffer; inc(i); dataStream.Position:=i; dataStream.Read(buffer,1); end; End;Function TWebModule1.GetFieldValueA(FieldName:string):string; var KeyStr:string;i:Integer; varbyte:byte; begin Result:=''; KeyStr:=djStr+char($0D)+Char($0A)+ 'Content-Disposition: form-data; name="'+fieldName+'"' ; i:=StrSearch(0,KeyStr,1); if i<0 then exit; self.DataStream.Position:=i+4; dataStream.Read(varbyte,1); while(varbyte<>$0D) do begin Result:=Result+char(varbyte); i:=i+1; dataStream.Position:=i+4; dataStream.Read(varbyte,1); end;end; Function TWebModule1.Pos_Reverse(var Key:string;SearchLen:integer=-1):integer; var I,K:Integer; //2000-12-14 KLen:Integer; ch:Char; begin Result:=0; if SearchLen<0 then I:=Self.DataStream.Size else I:=SearchLen; K:=Length(Key); KLen:=K; while(I>=0) do begin Self.DataStream.Position:=I; Self.DataStream.Read(Ch,1); if Ch=Key[K] then Dec(k) else begin I:=I-KLen+K; K:=KLen; end; Dec(I); if K=0 then begin Inc(I); Result:=I;Exit end; end;//while End; Procedure TWebModule1.FileToSave_Test(fileName:string); var bgPos:Integer; bgEnd:Integer; I:Integer; buffer:Pchar; F:File; begin Self.GetFilePos(bgPos); I:=Self.DataStream.Size-200; bgEnd:=Self.Pos_Reverse(djstr,I)-2; if bgEnd<=bgPos then exit; GetMem(Buffer,bgEnd-bgPos); DataStream.Position:=bgPos; DataStream.Read(Buffer^,bgEnd-bgPos); AssignFile(F,FileName); ReWrite(F,1); BlockWrite(F,Buffer^,bgEnd-bgPos); CloseFile(F); FreeMem(Buffer); end; procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); var kk:TStringList; I:integer; tempstr:string; begin Self.Initialize(); Self.GetFileContent(); // self.FileStream.SaveToFile('c:\AABB.jpg'); tempstr:= Self.GetFileNameValue('file1'); if tempstr<>'' then self.FileStream.SaveToFile('c:\TempAsp\'+ExtractFileName(tempstr)); Self.FileStream.Free(); // (test ok) FileToSave_Test('c:\'+ExtractFileName(tempstr)); kk:=tstringList.Create; kk.add('<html><head></head><body>'); kk.add('TotalBytes: '+IntToStr(Request.ContentLength)+'<p>'); kk.add('availableLength: '+IntToStr(Length(Request.Content))+'<p>'); kk.add('text1= '+GetFieldValueA('text1')+'<p>'); kk.add('text2= '+GetFieldValueA('text2')+'<p>'); kk.add('fileName= '+GetFileNameValue('file1')+'<p>' ); kk.add('fileLength= '+IntToStr(self.getfilePos(i))+'<hr>'); kk.add('CurrentTime: '+DateTimeToStr(now())); kk.Add('</body></html>'); Response.Content:=kk.Text; kk.free; Self.DataStream.Free();end;end.
<!这是客户端> <html> <head> <meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <title></title> <script ID="clientEventHandlersJS" LANGUAGE="javascript"> <!-- // var win2 function YS_onclick() { var i,s,ss if (window.FORM1.file1.value.length<1) { alert("请选则一个图片文件");window.FORM1.file1.focus();return false; } ss=window.FORM1.file1.value.split("\\") s=""; for (i=0;i<ss.length-1;i++) s=s+ss[i]+"\\" s=s+ss[ss.length-1] win2=open("","","toolbar=no,scrollbars=yes,resizable=yes"); win2.document.open("image/Text"); win2.document.writeln("<h>"+" 文件名: " +window.FORM1.file1.value+"</h> "); win2.document.writeln("<input type=button value='关闭窗口'"); win2.document.writeln("LANGUAGE=javascript onclick=window.close()></center><hr>"); // win2.document.writeln(" <b onmouseover=style.cursor='hand'"); // win2.document.writeln("onclick=window.close()><关闭窗口></b><hr>") win2.document.writeln("<img src="+s+">"); win2.document.close(); }function button1_onclick() { if (window.FORM1.file1.value.length<1) { alert("请选则一个图片文件");window.FORM1.file1.focus();return false; } document.images["IMG1"].src=window.FORM1.file1.value; }function FORM1_onsubmit() {
原理及主要方法准备一下,就贴出来
我想要,[email protected].谢谢
unit Unit1; //2000-12-13interfaceuses
Windows, Messages, SysUtils, Classes, HTTPApp;type
TWebModule1 = class(TWebModule)
procedure WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
DataStream:TMemoryStream; //全部
FileStream:TMemoryStream;//文件内容
DjStr:String;
function StrSearch(sPos:Integer;keystr:string;sg:Integer):Integer;
function Pos_Reverse(var Key:string;SearchLen:integer=-1):integer;
Procedure FileToSave_Test(fileName:string);
function GetFilePos(var bgPos:Integer):Integer;
function GetFileContent():integer;
function GetFileNameValue(FieldName:string):string;
Function GetFieldValueA(FieldName:string):string;
Procedure Initialize();
end;var
WebModule1: TWebModule1;implementation{$R *.DFM}Procedure TWebModule1.Initialize();
var i:Integer;
Buffer:char;
pBuffer:pchar;
extBuffer:pchar;
ActualReadCount:integer;
Totalbytes,Availablebytes:Integer;
begin Self.DataStream:=TMemoryStream.Create();
Self.FileStream:=TMemoryStream.Create(); TotalBytes:=Request.ContentLength;
AvailableBytes:=Length(Request.Content);
pBuffer:=PChar(Request.Content);
Self.DataStream.Write(pBuffer^,AvailableBytes); if TotalBytes>AvailableBytes then
begin
AvailableBytes:=TotalBytes-AvailableBytes;
GetMem(ExtBuffer,AvailableBytes);
repeat
ActualReadCount:=Request.ReadClient(extBuffer^,Availablebytes);
Self.DataStream.Write(extBuffer^,ActualReadCount);
AvailableBytes:=AvailableBytes-ActualReadCount;
until AvailableBytes<1; FreeMem(extBuffer);
end; i:=0;
Self.DjStr:='';
DataStream.Position:=0;
Repeat
DataStream.Read(Buffer,1);
if Buffer=#13 then break;
DjStr:=DjStr+Buffer;
inc(i);
DataStream.Position:=i;
until Buffer=#13;
end;
function TWebModule1.StrSearch(sPos:Integer;keystr:string;sg:Integer):Integer;
var i,k,strLen:Integer;
ErrmsgLength:Integer;
Buffer:char;
begin
K:=0;
i:=sPos;
Self.DataStream.Position:=i;
Self.DataStream.Read(Buffer,1);
strLen:=Length(KeyStr);
ErrMsgLength:=Self.DataStream.Size;
while(i<ErrMsgLength) do
begin
if buffer=KeyStr[K+1] then inc(k) else
begin
i:=i-k;
k:=0;
end;
inc(i);
if k=strLen then
begin
if sg=0 then result:=i-k else result:=i; exit;
end;
Self.DataStream.Position:=i;
Self.DataStream.Read(Buffer,1);
end;//while end;
result:=-1;
End;function TWebModule1.GetFilePos(var bgPos:Integer):Integer;
var i,inted:Integer;
begin
result:=-1;
i:=strsearch(0,'Content-Type:',1);
if i<0 then exit;
i:=strsearch(i,char($0d)+char($0A)+char($0d)+Char($0a),1);
if i<0 then exit;
bgPos:=i;//file stratr position
inted:=strsearch(i,djstr,0)-3;
if i<0 then exit;
Result:=Inted-i+1;
End; function TWebModule1.GetFileContent():integer;
var Buffer:Pchar;
bgPos,Len:Integer;
begin
fileStream.SetSize(0);
fileStream.Position:=0;
result:=0;
Len:=Self.GetFilePos(bgPos);
if Len<=bgPos then exit;
try
GetMem(Buffer,Len);
DataStream.Position:=bgPos;
DataStream.Read(Buffer^,Len);
FileStream.Write(Buffer^,Len);
finally
FreeMem(Buffer);
end;
end;Function TWebModule1.GetFileNameValue(FieldName:string):string;
var KeyStr:string;i:Integer;
// varbyte:byte;
Buffer:char;
begin
Result:='';
KeyStr:=djStr+char($0D)+Char($0A)+
'Content-Disposition: form-data; name="'+fieldName+'"' ;
i:=StrSearch(0,KeyStr,1);
if i<0 then exit;
i:=StrSearch(i,'filename="',1);
if i<0 then exit;
DataStream.Position:=i;
DataStream.Read(Buffer,1);
if Buffer='"' then exit;
while(buffer<>'"') do
begin
Result:=Result+Buffer;
inc(i);
dataStream.Position:=i;
dataStream.Read(buffer,1);
end;
End;Function TWebModule1.GetFieldValueA(FieldName:string):string;
var KeyStr:string;i:Integer;
varbyte:byte;
begin
Result:='';
KeyStr:=djStr+char($0D)+Char($0A)+
'Content-Disposition: form-data; name="'+fieldName+'"' ;
i:=StrSearch(0,KeyStr,1);
if i<0 then exit;
self.DataStream.Position:=i+4;
dataStream.Read(varbyte,1);
while(varbyte<>$0D) do
begin
Result:=Result+char(varbyte);
i:=i+1;
dataStream.Position:=i+4;
dataStream.Read(varbyte,1);
end;end;
Function TWebModule1.Pos_Reverse(var Key:string;SearchLen:integer=-1):integer;
var I,K:Integer; //2000-12-14
KLen:Integer;
ch:Char;
begin
Result:=0;
if SearchLen<0 then I:=Self.DataStream.Size else I:=SearchLen;
K:=Length(Key);
KLen:=K;
while(I>=0) do begin
Self.DataStream.Position:=I;
Self.DataStream.Read(Ch,1);
if Ch=Key[K] then Dec(k) else
begin
I:=I-KLen+K;
K:=KLen;
end;
Dec(I);
if K=0 then begin Inc(I); Result:=I;Exit end;
end;//while
End; Procedure TWebModule1.FileToSave_Test(fileName:string);
var bgPos:Integer;
bgEnd:Integer;
I:Integer;
buffer:Pchar;
F:File;
begin
Self.GetFilePos(bgPos);
I:=Self.DataStream.Size-200;
bgEnd:=Self.Pos_Reverse(djstr,I)-2;
if bgEnd<=bgPos then exit;
GetMem(Buffer,bgEnd-bgPos);
DataStream.Position:=bgPos;
DataStream.Read(Buffer^,bgEnd-bgPos);
AssignFile(F,FileName);
ReWrite(F,1);
BlockWrite(F,Buffer^,bgEnd-bgPos);
CloseFile(F);
FreeMem(Buffer);
end;
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var kk:TStringList;
I:integer;
tempstr:string;
begin Self.Initialize();
Self.GetFileContent();
// self.FileStream.SaveToFile('c:\AABB.jpg');
tempstr:= Self.GetFileNameValue('file1');
if tempstr<>'' then
self.FileStream.SaveToFile('c:\TempAsp\'+ExtractFileName(tempstr));
Self.FileStream.Free();
// (test ok) FileToSave_Test('c:\'+ExtractFileName(tempstr)); kk:=tstringList.Create;
kk.add('<html><head></head><body>');
kk.add('TotalBytes: '+IntToStr(Request.ContentLength)+'<p>');
kk.add('availableLength: '+IntToStr(Length(Request.Content))+'<p>');
kk.add('text1= '+GetFieldValueA('text1')+'<p>');
kk.add('text2= '+GetFieldValueA('text2')+'<p>');
kk.add('fileName= '+GetFileNameValue('file1')+'<p>' );
kk.add('fileLength= '+IntToStr(self.getfilePos(i))+'<hr>');
kk.add('CurrentTime: '+DateTimeToStr(now()));
kk.Add('</body></html>');
Response.Content:=kk.Text;
kk.free;
Self.DataStream.Free();end;end.
<html>
<head>
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<title></title>
<script ID="clientEventHandlersJS" LANGUAGE="javascript">
<!--
// var win2
function YS_onclick() {
var i,s,ss
if (window.FORM1.file1.value.length<1) { alert("请选则一个图片文件");window.FORM1.file1.focus();return false; }
ss=window.FORM1.file1.value.split("\\")
s="";
for (i=0;i<ss.length-1;i++) s=s+ss[i]+"\\"
s=s+ss[ss.length-1] win2=open("","","toolbar=no,scrollbars=yes,resizable=yes");
win2.document.open("image/Text");
win2.document.writeln("<h>"+" 文件名: " +window.FORM1.file1.value+"</h> ");
win2.document.writeln("<input type=button value='关闭窗口'");
win2.document.writeln("LANGUAGE=javascript onclick=window.close()></center><hr>");
// win2.document.writeln(" <b onmouseover=style.cursor='hand'");
// win2.document.writeln("onclick=window.close()><关闭窗口></b><hr>")
win2.document.writeln("<img src="+s+">");
win2.document.close();
}function button1_onclick() {
if (window.FORM1.file1.value.length<1) { alert("请选则一个图片文件");window.FORM1.file1.focus();return false; }
document.images["IMG1"].src=window.FORM1.file1.value;
}function FORM1_onsubmit() {
return true;
}
//-->
</script>
</head>
<body bgColor=skyblue>
<center><h1> ISAPI 文件上传范例</h1></center><hr
style="HEIGHT: 2px; LEFT: 79px; POSITION: absolute; TOP: 52px; WIDTH: 517px; Z-INDEX: 109">
<TABLE border=0 id=t1 cellPadding=0 cellSpacing=5 style="HEIGHT: 191px; WIDTH: 418px"
width="75%">
<TR>
<TD> <IMG Name="IMG1" alt="" src="" style="HEIGHT: 175px; LEFT: 34px; POSITION: absolute; TOP: 70px; WIDTH: 270px; Z-INDEX: 104"
border=1> </TD>
<TD>
<center> <INPUT id=button1 name=button1 type=button value=原始图象 onclick="YS_onclick()" style="LEFT: 74px; POSITION: absolute; TOP: 255px; Z-INDEX: 100">
</center>
</TD>
</TR>
</TABLE>
<FORM action="Test.dll" id=FORM1 method=post name=FORM1 enctype="multipart/form-data" LANGUAGE=javascript onsubmit="return FORM1_onsubmit()">
<HR
style="HEIGHT: 2px; LEFT: 79px; POSITION: absolute; TOP: 52px; WIDTH: 517px; Z-INDEX: 109">
<INPUT id=button1 language=javascript name=button1 onclick="return button1_onclick()" style="FONT-SIZE: smaller; LEFT: 169px; POSITION: absolute; TOP: 255px; Z-INDEX: 103" type=button value=刷新图片>
<INPUT id=text2 name=text2 style="HEIGHT: 22px; LEFT: 384px; POSITION: absolute; TOP: 124px; WIDTH: 239px; Z-INDEX: 106" value=Text2><BR>
<INPUT id=file1 name=file1 type=file style="HEIGHT: 22px; LEFT: 386px; POSITION: absolute; TOP: 162px; WIDTH: 254px; Z-INDEX: 101" >
<INPUT id=text1 name=text1 style="HEIGHT: 22px; LEFT: 384px; POSITION: absolute; TOP: 86px; WIDTH: 241px; Z-INDEX: 105" value=Text1>
<p>
<INPUT id=submit1 name=submit1 style="HEIGHT: 24px; LEFT: 498px; POSITION: absolute; TOP: 212px; WIDTH: 61px; Z-INDEX: 108" type=submit value=提交>
上传后的
<INPUT id=reset1 name=reset1 style="HEIGHT: 24px; LEFT: 406px; POSITION: absolute; TOP: 212px; WIDTH: 51px; Z-INDEX: 107" type=reset value=重置>文件存放
C:\TempAsp\XXX.XX</p>
</FORM>
</body>
</html>
(以前我也提问过,就是没人应,搞得很伤心)