Ver Mensaje Individual
  #9  
Antiguo 17-08-2008
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Cita:
Empezado por roman Ver Mensaje
Hey, pensé que ibas a poner la solución usando wininet
Si es que entro al trapo enseguida

Aquí tienes el código roman, pero aviso que esta bastante sucio y tiene algo de copy&paste de otros proyectos, pero aquí es la una de la madrugada (mas bien una y media) y no voy a dedicarle mas tiempo

Código Delphi [-]
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
          // Agregamos el campo
          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));
          // Agregamos el archivo
          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;
                // Esto se lo copio a roman
                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;
                // Fin del plagio
              finally
                FreeMem(Buffer);
              end;
            finally
              Response.Free;
            end;
          end;
        finally
          Data.Free;
        end;
        InternetCloseHandle(hReq);
      end;
      InternetCloseHandle(hCon);
    end;
    InternetCloseHandle(hNet);
  end;
end;

Última edición por seoane fecha: 17-08-2008 a las 00:40:57.
Responder Con Cita