Einzelnen Beitrag anzeigen

franktron

Registriert seit: 11. Nov 2003
Ort: Oldenburg
1.446 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#1

Preoblem mit Dateiupload PHP>Delphi

  Alt 18. Jul 2013, 09:01
Ich lade per PHP eine Datei zu meine Delphi Programm hoch (per IDHTTPServer)

Code:
function uploadplotterfile($mode,$id,$filename) {
   global $localpath;
   define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
   $header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
   define('FORM_FIELD', 'uploaded_file');
   
   if (!is_file($localpath. '/' .$filename)) {
      echo 'Error ' . $localpath. '/' .$filename;
   }
   
   $file_contents = file_get_contents($localpath. '/' .$filename);  
   
   $content = "--".MULTIPART_BOUNDARY."\r\n".
                     "Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n".
                     "Content-Type: application/pdf\r\n\r\n".
                     $file_contents."\r\n".
   
   /*// add some POST fields to the request too: $_POST['foo'] = 'bar'
   $content .= "--".MULTIPART_BOUNDARY."\r\n".
                     "Content-Disposition: form-data; name=\"foo\"\r\n\r\n".
                     "bar\r\n".*/
   
   // signal end of request (note the trailing "--")
   $content .= "--".MULTIPART_BOUNDARY."--\r\n";
   $context = stream_context_create(array(
      'http' => array(
      'method' => 'POST',
      'header' => $header,
      'content' => $content,
      ),
   ));
   file_get_contents('http://192.168.11.40:4999/plotter?mode=' . $mode . '&id=' . $id,false,$context);
}
Delphi-Quellcode:
procedure TPlotterWebMain.OnPost(Request: TIdHTTPRequestInfo;
  Response: TIdHTTPResponseInfo);
Var
  Stream : TFileStream;
  Decoder : TIdMessageDecoder;
  MsgEnd : Boolean;
  Boundary: String;
  Buffer : String;
begin
  inherited;
  If Request.Params.Count>1 then
  Begin
    If Request.Params.Strings[0]='mode=uploadthen
    Begin
      Buffer:=Request.Params.Strings[1];
      Delete(Buffer,1,Pos('=',Buffer));
      MsgEnd:=False;
      Stream:=TFileStream.Create(Buffer+'.pdf',fmCreate);
      Decoder:=TIdMessageDecoderMIME.Create(nil);
      Boundary := ExtractHeaderSubItem(Request.ContentType, 'boundary', QuoteHTTP);
      TIdMessageDecoderMIME(Decoder).MIMEBoundary := Boundary;
      Decoder.FreeSourceStream := False;
      Decoder.SourceStream := Request.PostStream;
      //Decoder.ReadLn();
      Decoder.ReadHeader;
      if Decoder.PartType=mcptAttachment then
        Decoder.ReadBody(Stream,MsgEnd);

      FreeAndNil(Stream);
    End;
  End;
  Response.ResponseNo:=200;
end;
Das Klappt auch aber in der Datei die ich auf dem Windows PC Speichere sind nach jeder Zeile ein HEX(0A) und ich weis nicht warum kann mir jemand helfen.
Frank
Tux sein Lieblingsquellcode
While anzfische<TuxSatt do begin
Fisch:=TFisch.Create; Tux.EssenFisch(Fisch); Fisch.Free;inc(anzfische); end;
  Mit Zitat antworten Zitat