Einzelnen Beitrag anzeigen

Hobbycoder

Registriert seit: 22. Feb 2017
930 Beiträge
 
#1

TIdHttpServer JPG-Upload von HTML

  Alt 15. Jul 2019, 12:14
Hi,

ich möchte gerne in meinem TIdHTTPServer über ein HTML-Formular Bilder/Dateien hochladen, die dieser dann speichert.
Um die Dateien aus dem Stream zu extrahieren habe ich mir (zugegeben im Inet geklaut, woher weiß ich nicht mehr) folgende Methode:
Delphi-Quellcode:
procedure TfrmWSMain.ExtractFiles(var ARequestInfo: TIdHTTPRequestInfo;
  Files: TStringList);
var
  ms : TMemoryStream;
  newdecoder, Decoder: TIdMessageDecoder;
  boundary, startboundary : String;
  msgEnd : Boolean;
  tmp : String;
  I : Integer;
  fname : String;
  tsValues : TStringList;
begin
  if Pos('krank', LowerCase(ARequestInfo.Document)) > 0 then
  begin
    if ARequestInfo.PostStream<>nil then
    begin
      ARequestInfo.PostStream.Position:=0;
      msgEnd:=False;
      boundary:=ExtractHeaderSubItem(ARequestInfo.ContentType, 'boundary', QuoteHTTP);
      startboundary := '--' + boundary;
      repeat
        tmp:=ReadLnFromStream(ARequestInfo.PostStream, -1, True);
      until tmp=startboundary;
      Decoder:=TIdMessageDecoderMIME.Create(nil);
      TIdMessageDecoderMIME(Decoder).MIMEBoundary:=boundary;
      tsValues:=TStringList.Create;
      try
        repeat
          Decoder.SourceStream:=ARequestInfo.PostStream;
          Decoder.FreeSourceStream:=False;
          Decoder.ReadHeader;
          Inc(i);
          case Decoder.PartType of
            mcptText, mcptAttachment:
              begin
                ms:=TMemoryStream.Create;
                ms.Position:=0;
                newdecoder:=Decoder.ReadBody(ms, msgEnd);
                tmp:=Decoder.Headers.Text;
                fname:=Decoder.Filename;
                if Decoder<>nil then
                  TIdMessageDecoderMIME(Decoder).MIMEBoundary:=boundary;
                Sleep(100);
                if fname<>'then
                begin
                  ms.SaveToFile(fname);
                  //msgEnd:=True;
                end else begin
                  ms.SaveToFile(IntToStr(i)+'.txt');
                end;
                ms.Free;
              end;
            mcptIgnore:
              begin
                try
                  FreeAndNil(Decoder);
                  Decoder:=TIdMessageDecoderMIME.Create(nil);
                  TIdMessageDecoderMIME(Decoder).MIMEBoundary:=boundary;
                finally
                  ms.Free;
                end;
              end;
            mcptEOF:
              begin
                FreeAndNil(Decoder);
                msgEnd:=True;
              end;
          end;
        until (Decoder = nil) or (msgEnd);
      finally
        if Decoder<>nil then
          Decoder.Free;
      end;
    end;
  end;
end;
die grundsätzlich erst mal funktioniert.

HTML sieht so aus:
Code:
            <tr><form data-ajax="false" id="krank" method="post" action="krank.php" enctype="multipart/form-data">
                      <td><label>Krank von</label></td>
                 <td><input type="text" name="krankvon" id="krankvon" style="width:100%;" focus></td>            
            </tr><tr>
                      <td><label>Krank bis</label></td>
                 <td><input type="text" name="krankbis" id="krankbis" style="width:100%;" focus></td>            
            </tr><tr>
                      <td><label>Bild 1</label></td>
                  <td><input type="file" name="datei1" id="datei1" accept="text/*"></td>
            </tr><tr>
                      <td><label>Bild 2</label></td>
                  <td><input type="file" name="datei1" id="datei2" accept="text/*"></td>
            </tr><tr>
                      <td><label>Bild 3</label></td>
                  <td><input type="file" name="datei1" id="datei3" accept="text/*"></td>
            </tr><tr>
            <td colspan="2">
                 <input type="submit" value="Absenden" style="width:100%;">
            </td>
            </tr></form>
ich bekomme für jedes Input die Daten in eine Datei.

2 Probleme habe ich noch, welches ich selbst nicht lösen kann:

1. Dateiname der Dateien wird nicht erkannt und
2. die hochgeladenen JPG's sind fehlerhaft.

Die Dateigröße ist geringfügig größer. Soweit ich das mit einem Hexeditor erkannt habe, wird wohl aus einem LF ein CRLF. Damit verändert sich der Inhalt und das Bild wird nicht mehr korrekt dargestellt.

Wer kann mir bei der Lösung helfen?
Gruß Hobbycoder
Alle sagten: "Das geht nicht.". Dann kam einer, der wusste das nicht, und hat's einfach gemacht.
  Mit Zitat antworten Zitat