在我的 HTTP 服务器上,我试图允许用户通过这种形式上传图片:
<form action="/?command=saveImage" method="post" enctype="multipart/form-data">
<input type="file" name="images" multiple="multiple"/>
<input type="submit">
</form>
我正在使用此来源上传图像:http ://embarcadero.newsgroups.archived.at/public.delphi.internet.winsock/201107/1107276163.html并更改了 ProcessMimePart 程序,以便以正确的格式保存图像:
procedure ProcessMimePart(var aDecoder: TIdMessageDecoder;
var aMsgEnd: Boolean);
var
LMStream: TMemoryStream;
LNewDecoder: TIdMessageDecoder;
fileName, fileExtension: string;
begin
fileName := aDecoder.fileName;
fileExtension := GetFileExtension(fileName);
if (fileExtension <> 'jpg') and (fileExtension <> 'png')
and (fileExtension <> 'bmp') then
begin
Exit;
end;
LMStream := TMemoryStream.Create;
try
LNewDecoder := aDecoder.ReadBody(LMStream, aMsgEnd);
try
LMStream.Position := 0;
TSaveImageController.WriteImage(fileName, fileExtension, LMStream);
except
LNewDecoder.Free;
raise;
end;
aDecoder.Free;
aDecoder := LNewDecoder;
finally
FreeAndNil(LMStream);
end;
end;
当我上传一个文件时,此代码工作正常,但是当我尝试上传更多图像时,我保存了第一个图像,我得到:'模块'App.exe'中地址 004BAD86 的访问冲突。读取地址 00000000'
编辑:
function GetFileExtension(aFileName: string): string;
var
isValidImageFileExtension: Boolean;
lastIndexOfComa: integer;
fileExtension: string;
begin
lastIndexOfComa := aFileName.LastIndexOf('.');
if lastIndexOfComa <= 0 then
begin
Result := '';
end
else
begin
fileExtension := LowerCase(aFileName.Substring(lastIndexOfComa + 1));
if (fileExtension = 'jpg') or (fileExtension = 'bmp') or
(fileExtension = 'png') then
begin
Result := fileExtension;
Exit;
end
else
begin
Result := '';
end;
end;
end;