| Код |
function EncodeBase64(Value: String): String; const b64alphabet: PChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; pad: PChar = '====';
function EncodeChunk(const Chunk: String): String; var W: LongWord; i, n: Byte; begin n := Length(Chunk); W := 0; for i := 0 to n - 1 do W := W + Ord(Chunk[i + 1]) shl ((2 - i) * 8); Result := b64alphabet[(W shr 18) and $3f] + b64alphabet[(W shr 12) and $3f] + b64alphabet[(W shr 06) and $3f] + b64alphabet[(W shr 00) and $3f]; if n <> 3 then Result := Copy(Result, 0, n + 1) + Copy(pad, 0, 3 - n); //add padding when out len isn't 24 bits end;
begin Result := ''; while Length(Value) > 0 do begin Result := Result + EncodeChunk(Copy(Value, 0, 3)); Delete(Value, 1, 3); end; end;
procedure TForm1.Button1Click(Sender: TObject); var StringStream: TStringStream; PostVars: TStringList; FileStream: TFileStream; begin PostVars := TStringList.Create; StringStream := TStringStream.Create(''); FileStream := TFileStream.Create('C:\tatu.jpg', fmOpenRead or fmShareDenyWrite); try IdHTTP1.Connect; StringStream.CopyFrom(FileStream, FileStream.Size); PostVars.Values['file'] := EncodeBase64(StringStream.DataString); IdHTTP1.Post('http://www.domain.de/upload/upload.php?pDatei=tatu.jpg', PostVars, StringStream); finally StringStream.Free; PostVars.Free; FileStream.Free; end; end;
|
upload.php:
| Код |
$datei = base64_decode($file); $fp = fopen($HTTP_GET_VARS['pDatei'], "w"); fwrite($fp, $datei); fclose($fp);
|
ничего не получается. в чем трабл ?
данные нормально поступают в mime на сайт. патом я их перекодирую обратно base64_decode($file); после перекодировки файл на ~1% по размеру становится меньше и не открывается  (pad: PChar = '===='; - что это ?) если pad: PChar = ''; так что раскодирование происходит но все равно файл получается некоррректный в чем же дело ? может кто знает альтернативную функцию кодирования jpeg в base 64 ?
имо может я что то криво делаю ?
|