我写的是ASP的上传组件,这段是生成原图的函数
function TFileUpload.getitem2(const bufchar;
const filesize:integer):boolean;
const
head='Content-Disposition: form-data; name="';//上传图片的头数据标记
filetag='"; filename="';//上传图片的地址标记
file_type='Content-Type: ';上传文件的数据类型标记
var
curr:integer;
taglen:integer;
tag:array[0..50] of char;
temp:array[0..300] of char;
i,k,p,t:integer;
savefile,isfile:boolean;
fs:Tfilestream;
filename,Rndname:string;
function seekstring(from:integer;strchar):integer;
var //扫描指定字符串
i:integer;
begin
result:=-1;
for i:=from to filesize-1 do
if buf=str^ then
if strLcomp(str,pchar(@buf),strlen(str))=0 then
begin
result:=i;
break;
end;
end;
function seektag(from:integer):integer;
var //快速扫描标记
i:integer;
begin
result:=-1;
for i:=from to filesize-1 do
if pdword(@buf)^=$2d2d2d2d then //$2d2d2d2d ='----'
if strLcomp(tag,pchar(@buf),taglen)=0 then
begin
result:=i;
break;
end;
end;
begin
curr:=0;
tag[0]:=#0;
t:=seekstring(0,#13#10);
taglen:=t;
strlcopy(tag,buf,taglen);
curr:=taglen+2;
while curr+2<filesize do
if strlcomp(head,@buf[curr],strlen(head))=0 then
begin //检查表单域标记
curr:=curr+strlen(head);
t:=seekstring(curr,'"');
strlcopy(temp,@buf[curr],t-curr);
Inputs[fcount].name:=strpas(temp);//获取表单域名
isfile:=false;
if strlcomp(@buf[t],filetag,strlen(filetag))=0 then
begin //若是文件
t:=t+strlen(filetag);
k:=seekstring(t,'"');
strlcopy(temp,@buf[t],k-t);
isfile:=true;
Inputs[fcount].value:=extractfilename(temp); //文件名
t:=seekstring(k,file_type)+strlen(file_type);
curr:=seekstring(t,#13#10);
strlcopy(temp,@buf[t],curr-t);
Inputs[fcount].filetype:=strpas(temp);//文件类型
curr:=curr+4;
end else
curr:=t+5;
t:=seektag(curr); //扫描下个标记
if not isfile then
begin
buf[t-2]:=#0;
Inputs[fcount].value:=strpas(@buf[curr]);//表单域值
inc(fcount);
curr:=t+2+taglen;
end else
begin
Rndname:=RadomFileName+ExtractFileExt(Inputs[fcount].value) ;
filename:=savepath+Rndname;
Inputs[fcount].filesize:=t-2-curr; //文件大小
savefile:=fileexists(filename) and not foverwrite;
if (length(Inputs[fcount].value)>0) and not savefile then
try
fs:=Tfilestream.create(filename,fmCreate); //生成文件
fs.Writebuffer(buf[curr],t-2-curr);
finally
fs.Free;
end;
inc(fcount);
curr:=t+2+taglen;
end;
end else
exit;
end;