uses
WinInet;
const
strSeparador = 'Separador528C65E88C1F4DB5BDB11BF1558AF1C3';
function UploadImage(Filename: String): String;
var
hNet: HINTERNET;
hCon: HINTERNET;
hReq: HINTERNET;
Context: DWORD;
BytesRead: DWORD;
Data: TMemoryStream;
Response: TStringStream;
Buffer: PChar;
Str: String;
FileStream: TFileStream;
i: Integer;
begin
Result:= EmptyStr;
Context:= 0;
hNet := InternetOpen('Agente', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if (hNet <> nil) then
begin
hCon:= InternetConnect(hNet,'img.tomatone.net',80,nil,nil,
INTERNET_SERVICE_HTTP,0,Context);
if (hCon <> nil) then
begin
hReq:= HttpOpenRequest(hCon,'POST','/index.php',nil,nil,nil,
INTERNET_FLAG_RELOAD,Context);
if (hReq <> nil) then
begin
Data:= TMemoryStream.Create;
try
Str:= '--' + strSeparador + #13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
Str:= 'Content-Disposition: form-data; name="comp"'
+ #13#10#13#10 + 'ok' + #13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
Str:= '--' + strSeparador + #13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
Str:= 'Content-Disposition: file; name="userfile"; filename="' + ExtractFilename(Filename) + '"' + #13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
Str:= 'Content-Type: image/jpeg' + #13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
Str:= 'Content-Transfer-Encoding: binary' + #13#10#13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
FileStream:= TFileStream.Create(Filename,fmOpenRead);
try
Data.CopyFrom(FileStream,0)
finally
FileStream.Free;
end;
Str:= #13#10 + '--' + strSeparador + '--' + #13#10;
Data.WriteBuffer(PChar(Str)^,Length(Str));
if HttpSendRequest(hReq,
PChar('Content-Type: multipart/form-data; boundary='
+ strSeparador), Cardinal(-1), Data.Memory,Data.Size) then
begin
Response:= TStringStream.Create(EmptyStr);
try
GetMem(Buffer,32*1024);
try
while (InternetReadFile(hReq,Buffer,32*1024,BytesRead)) do
begin
if (BytesRead = 0) then
break;
Response.Write(Buffer^,BytesRead)
end;
Str:= Response.DataString;
I := Pos('href=''big/', Str);
if I > 0 then
begin
Delete(Str, 1, I + 9);
I := Pos('''', Str);
if I > 0 then
begin
Result := 'http://img.tomatone.net/big/' + Copy(Str, 1, I - 1);
end;
end;
finally
FreeMem(Buffer);
end;
finally
Response.Free;
end;
end;
finally
Data.Free;
end;
InternetCloseHandle(hReq);
end;
InternetCloseHandle(hCon);
end;
InternetCloseHandle(hNet);
end;
end;