Uploading Image to web folder

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ELI
Posts: 1
Joined: Wed 29 Jan 2014 22:57
Location: Israel
Contact:

Uploading Image to web folder

Post by ELI » Wed 10 Mar 2021 12:51

I need to upload images programmatically to my web folder from delphi VCL Database program.

I was trying the following that I found on the web but it never worked:

procedure TUserFrame.UploadBtnClick(Sender: TObject);
var

PostData: TStream;
Response: string;
begin

PostData := TFileStream.Create('c:\delphiprog\projects\eligeri.jpg', fmOpenRead or fmShareDenyWrite);

Response := IdHTTP1.Post(URL, PostData);

if IdHTTP1.ResponseCode = 200 then begin
WriteLn('Response: ' + Response);
end else begin
WriteLn('Error: ' + IdHTTP1.ResponseText);
end;

PostData.Free;
end;

I will be glad to get reply.

Thanks in advance.

Eliyahu Neria

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Uploading Image to web folder

Post by ViktorV » Fri 12 Mar 2021 15:24

To upload a file to the site using the TScHttpWebRequest component https://www.devart.com/sbridge/docs/tsc ... equest.htm, you can use the following code:

Code: Select all

var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  ResponseStr: string;
  Stream: TFileStream;
begin
  Request := TScHttpWebRequest.Create(URL);
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    Request.Method := rmPOST;

    Request.ContentType := 'image/jpeg';
    Request.ContentLength := Stream.Size;
    Request.WriteData(Stream);

    Response := Request.GetResponse;
    ResponseStr := Response.ReadAsString;
    Response.Free;
  finally
    Stream.Free;
    Request.Free;
  end;
end;

Post Reply