T
T
TotTip2020-10-12 20:32:48
Delphi
TotTip, 2020-10-12 20:32:48

How to send post with file and parameter?

Hello!
You need to send a post request to the server (php) and pass in it the parameter user (string) and image (file)
the image is stored in TImage (this is a screenshot)
I prepare the image like this:

the code

image_ms := TMemoryStream.Create;
    jpg := TJPEGImage.Create;
    jpg.Assign(Image1.Picture.Graphic);
    Image1.Free;
    jpg.CompressionQuality := 80;
    jpg.Compress;
    jpg.SaveToStream(image_ms);


how to send a request without a file sort of figured out:
the code

Stream := TIdMultipartFormDataStream.Create;
    Stream.AddFormField('user', user_id);
    result := IdHTTP1.Post('https://site.ru/upload.php', Stream);
    Stream.Free;


how to add image here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hemul GM, 2020-10-12
@TotTip

Depends on how the server accepts it. You can pass it inside MultipartFormDataStream, or you can send it in base64 format in the same MultipartFormDataStream.
And I recommend using not Indy, but Sytem.Net.HttpClient.
And in Indy, the file transfer happens like this:

MultipartFormDataStream.AddFile('file', 'C:\test.rar', 'application/octet-stream');
или
MultipartFormDataStream.AddFile('file', 'test.jpg', 'application/octet-stream', image_ms);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question