-->

如何与突触发送文件和其他POST数据(How to send a file AND other PO

2019-09-19 10:26发布

德尔福使用:2007。

你好,

我有两个文本输入和一个文件输入一个简单的网页。 现在,要发送的形式,无论是文本输入和文件输入需要填补。 与突触,我知道如何上传文件(HttpPostFile),以及如何发布数据( 列举HTTPMethod)。 不过,我不知道该怎么做两个。

看着突触的源代码后,我想我必须“格式化”与边界或类似的东西我的数据。 我想我应该有我的输入文件一个边界,并为我的文字输入的另​​一边界。 我发现关于这个问题的文章,但它是关于发送电子邮件附件。 我试图重现他们说的与突触,但没有任何结果。

代码HttpPostFile:

function HttpPostFile(const URL, FieldName, FileName: string;
  const Data: TStream; const ResultData: TStrings): Boolean;
var
  HTTP: THTTPSend;
  Bound, s: string;
begin
  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
  HTTP := THTTPSend.Create;
  try
    s := '--' + Bound + CRLF;
    s := s + 'content-disposition: form-data; name="' + FieldName + '";';
    s := s + ' filename="' + FileName +'"' + CRLF;
    s := s + 'Content-Type: Application/octet-string' + CRLF + CRLF;
    WriteStrToStream(HTTP.Document, s);
    HTTP.Document.CopyFrom(Data, 0);
    s := CRLF + '--' + Bound + '--' + CRLF;
    WriteStrToStream(HTTP.Document, s);
    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
    Result := HTTP.HTTPMethod('POST', URL);
    if Result then
      ResultData.LoadFromStream(HTTP.Document);
  finally
    HTTP.Free;
  end;
end;

谢谢。

Answer 1:

您的代码是接近。 你只是发送您的文件中的字段而不是文本字段。 要做到这三个,试试这个来代替:

function HttpPostFile(const URL, InputText1FieldName, InputText1, InputText2FieldName, InputText2, InputFileFieldName, InputFileName: string; InputFileData: TStream; ResultData: TStrings): Boolean; 
var 
  HTTP: THTTPSend; 
  Bound: string; 
begin 
  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary'; 
  HTTP := THTTPSend.Create; 
  try 
    WriteStrToStream(HTTP.Document,
      '--' + Bound + CRLF +
      'Content-Disposition: form-data; name=' + AnsiQuotedStr(InputText1FieldName, '"') + CRLF +
      'Content-Type: text/plain' + CRLF +
      CRLF); 
    WriteStrToStream(HTTP.Document, InputText1); 
    WriteStrToStream(HTTP.Document,
      CRLF +
      '--' + Bound + CRLF +
      'Content-Disposition: form-data; name=' + AnsiQuotedStr(InputText2FieldName, '"') + CRLF +
      'Content-Type: text/plain' + CRLF +
      CRLF); 
    WriteStrToStream(HTTP.Document, InputText2); 
    WriteStrToStream(HTTP.Document,
      CRLF +
      '--' + Bound + CRLF +
      'Content-Disposition: form-data; name=' + AnsiQuotedStr(InputFileFieldName, '"') + ';' + CRLF + 
      #9'filename=' + AnsiQuotedStr(InputFileName, '"') + CRLF +
      'Content-Type: application/octet-string' + CRLF +
      CRLF); 
    HTTP.Document.CopyFrom(InputFileData, 0); 
    WriteStrToStream(HTTP.Document,
      CRLF +
      '--' + Bound + '--' + CRLF); 
    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound; 
    Result := HTTP.HTTPMethod('POST', URL); 
    if Result then 
      ResultData.LoadFromStream(HTTP.Document); 
  finally 
    HTTP.Free; 
  end; 
end; 

如果切换到印第安纳波利斯,您可以使用它TIdMultipartFormDataStream类:

function HttpPostFile(const URL, InputText1FieldName, InputText1, InputText2FieldName, InputText2, InputFileFieldName, InputFileName: string; InputFileData: TStream; ResultData: TStrings): Boolean; 
var 
  HTTP: TIdHTTP; 
  Input: TIdMultipartFormDataStream;
  Output: TMemoryStream;
begin 
  Result := False;
  try
    Output := TMemoryStream.Create;
    try
      HTTP := TIdHTTP.Create; 
      try 
        Input := TIdMultipartFormDataStream.Create;
        try
          Input.AddFormField(InputText1FieldName, InputText1);
          Input.AddFormField(InputText2FieldName, InputText2);
          Input.AddFormField(InputFileFieldName, 'application/octet-stream', '', InputFileData, InputFileName);
          HTTP.Post(URL, Input, Output);
        finally
          Input.Free;
        end;
      finally
        HTTP.Free;
      end;
      Output.Position := 0;
      ResultData.LoadFromStream(Output);
      Result := True; 
    finally
      Output.Free;
    end;
  except
  end; 
end; 


Answer 2:

我在我的项目也使用突触。 为了让我的工作变得简单,并与突触更快,我写了THTTPSendEx类,让使用和代码和更多的功能最低的速度快。 目前,它是一个测试版本。

它的美景,如印。

创建THTTPSendEx类。 创建方法OnBeginWork,OnWork,OnWorkEnd从它原型(见PAS文件),并将其分配给创建的类。 这就是所有你所需要的,只是调用get,之类的POST功能。

我还实现了多-fomdata用于快速文件发布在该格式TMultipartFormDataStream类。 有了它,你可以很容易写文件和领域。

使用的例子:

var
 HTTP:THTTPSendEx;
 Data:TMultipartFormDataStream;
 sHTML:string; //Recived HTML code from web
begin
 HTTP:=THTTPSEndEx.Create;
 Data:=TMultipartFormDataStream.Create;
 try
  Data.AddFile('myFile','Path to the local file(No UNC paths)');
  Data.DataEnd;
  if HTTP.Post('URL HERE',Data,sHTML) then
  begin
  //Connection established
  //Check HTTP response
  if HTTP.IsSuccessfull then  //HTTP returns "200 OK" code.
  begin
    ShowMessage('File successfully posted to the server.');
  end;
  end else
  begin
   ShowMessage('Can not establish a connection to the server...'+#13+'Network is not avaliable or server socket does not exist.');
  end;
 finally
   FreeAndNil(HTTP);
   FreeAndNil(Data);
 end;
end;

你可以在我的网站看看吧 。

如果您有任何想法,请给它的项目页面评论。

对不起,我的英文错误。



Answer 3:

良好的阅读: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

问题解决了。 我一直在任何情况下的问题,公众有同样的问题一天。



文章来源: How to send a file AND other POST data with Synapse