Ver Mensaje Individual
  #2  
Antiguo 16-08-2008
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Reputación: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
Para ese caso específico:

Código Delphi [-]
uses IdHTTP, IdMultipartFormData;

{
  Sube una imagen al sitio http://img.tomatone.net/
  Devuelve la URL de la imagen o una cadena vacía en caso de error

  El sitio sólo acepta imágenes png, jpeg y gif
}
function UploadImage(FileName: String): String;
var
  IdHTTP: TIdHTTP;
  Stream: TIdMultipartFormDataStream;
  Ext: String;
  Response: String;
  I: Integer;

begin
  Result := '';

  Ext := Copy(ExtractFileExt(FileName), 2, MaxInt);
  if (Ext = 'jpg') then
    Ext := 'jpeg';

  IdHTTP := TIdHTTP.Create;
  Stream := TIdMultipartFormDataStream.Create;

  try
    Stream.AddFile('userfile', FileName, 'image/' + Ext);
    Stream.AddFormField('comp', 'ok');
    Response := IdHTTP.Post('http://img.tomatone.net/index.php', Stream);

    I := Pos('href=''big/', Response);
    if I > 0 then
    begin
      Delete(Response, 1, I + 9);
      I := Pos('''', Response);
      if I > 0 then
      begin
        Result := 'http://img.tomatone.net/big/' + Copy(Response, 1, I - 1);
      end;
    end;
  finally
    IdHTTP.Free;
    Stream.Free;
  end;
end;

// Saludos
Responder Con Cita