On my HTTP server I'm trying to allow users to upload images by this form:
<form action="/?command=saveImage" method="post" enctype="multipart/form-data">
<input type="file" name="images" multiple="multiple"/>
<input type="submit">
</form>
I'm using this source to upload an image: http://embarcadero.newsgroups.archived.at/public.delphi.internet.winsock/201107/1107276163.html and changed ProcessMimePart procedure in order to save the images in their correct format:
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;
This code works fine when I upload one file but when I try to upload more images I save the first image and the I get a: 'Access violation at address 004BAD86 in module 'App.exe'. Read of address 00000000'
EDIT:
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;