如何使用Synapse发送文件和其他POST数据 [英] How to send a file AND other POST data with Synapse

查看:236
本文介绍了如何使用Synapse发送文件和其他POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi使用: 2007。

您好,

我有一个简单的网页,有两个文本输入和一个文件输入。现在,对于要发送的表单,必须填写文本输入和文件输入。使用Synapse,我知道如何上传文件( HttpPostFile )以及如何发布数据( HttpMethod )。但是,我不知道如何做到这两点。

I have a simple web page with two text input and one file input. Now, for the form to be sent, both the text inputs and the file input have to be filled. With Synapse, I know how to upload a file (HttpPostFile) and how to post data (HttpMethod). However, I don't know how to do both.

在查看Synapse的源代码之后,我想我必须用边界或其他东西格式化我的数据像那样。我想我的输入文件应该有一个边界,我的文本输入应该有另一个边界。我发现了一篇关于这个主题的文章,但它是关于发送电子邮件附件的。我尝试重现他们用Synapse所说的内容,没有结果。

After looking at the source code of Synapse, I guess I have to "format" my data with boundaries or something like that. I guess I should have one boundary for my input file and another boundary for my text inputs. I found an article on the subject, but it's about sending email attachments. I tried to reproduce what they said with Synapse, with no results.

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;

谢谢。

推荐答案

您的代码已关闭。您只发送文件字段,但不发送文本字段。要做到这三点,请尝试这样做:

Your code is close. You are only sending your file field but not your text fields. To do all three, try this instead:

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; 

如果你切换到Indy,你可以使用它的 TIdMultipartFormDataStream class:

If you switch to Indy, you can use its TIdMultipartFormDataStream class:

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; 

这篇关于如何使用Synapse发送文件和其他POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆