В моем приложении VCL у меня есть http-сервер. Я пытаюсь загрузить изображение из браузера. Вот код процедуры commandGet:
procedure TForm6.HTTPServerCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
imageStream: TMemoryStream;
begin
if Pos('command=addImage', ARequestInfo.UnparsedParams) > 0 then
begin
AResponseInfo.ContentText := UploadImageForm;
end
else if Pos('command=saveImage', ARequestInfo.UnparsedParams) > 0 then
begin
imageStream := TMemoryStream.Create;
try
ARequestInfo.PostStream.Seek(0, soFromBeginning);
imageStream.LoadFromStream(ARequestInfo.PostStream);
imageStream.SaveToFile('picture.jpeg');
finally
imageStream.Free;
end;
end;
end;
Вот код UploadImageForm:
function UploadImageForm: string;
var
uploadImageHTMLForm: TStringBuilder;
begin
uploadImageHTMLForm := TStringBuilder.Create;
try
// by default enctype is application/x-www-form-urlencoded
// uploadImageHTMLForm.AppendLine('<form action="/?command=saveImage" method="post">');
// EDIT
uploadImageHTMLForm.AppendLine('<form action="/?command=saveImage" method="post" enctype="multipart/form-data">');
uploadImageHTMLForm.AppendLine('<input type="file" name="uploadField">');
uploadImageHTMLForm.AppendLine('<input type="submit">');
uploadImageHTMLForm.AppendLine('</form>');
Result := uploadImageHTMLForm.ToString;
finally
uploadImageHTMLForm.Free;
end;
end;
Проблема в том, что когда я выбираю файл .jpeg и нажимаю «Отправить», ARequestInfo.PostStream пуст (я создаю его onCreatePostStream). Любая идея, как это решить.