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
			
									
									
						Uploading Image to web folder
Re: Uploading Image to web folder
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;