c#.NET multipart / form-data上传文件

时间:2012-10-06 16:38:46

标签: c# .net http

我想创建一个应用程序来自动将文件上传到googles SearchbyImage-tool https://www.google.de/searchbyimage/upload

Google正在使用multipart / form-data来提交文件,如下所示:

Content-Type: multipart/form-data; boundary=---------------------------265001916915724
Content-Length: 9989
-----------------------------265001916915724
Content-Disposition: form-data; name="image_url"


-----------------------------265001916915724
Content-Disposition: form-data; name="encoded_image"; filename=""
Content-Type: application/octet-stream


-----------------------------265001916915724
Content-Disposition: form-data; name="image_content"

iVBORw0KGgoAAAANSUhEUgAAA7sAAAAdCAYAAABrN3uYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABovSURBVHhe7V15cF3Vefe00KHT0uk0_7QJk2nS0oLTNNOJaQspGQ-BBuOCgcYuBFLAAlK2sIQtwAcSHDuAbeyCV_C-IxvjRTIuCGN5k42RjLHlRdbmJ1mSV9larOVJv57lnnvPvffc5T0_6Uny92bu2E_vvnPP-c53vvP7vt_3nTcI9CIJkARIAiQBkgBJgCRwEUqgra0NiUQCx48fT_viYmtsbMR3fnAXrr52pPhXXfw9vUgCJAGSAEkgexIYlL1H05NJAiQBkgBJgCRAEiAJ9H8JcGd3MHNyrxn2qPhXXfw9vUgCJAGSAEkgexIYxA00XSQD0gHSAdIB0gHSAdIB0oH0dUB3cjmjq95nD-LRk0kCJAGSAElg0OAf_BczyPKSEUl6T_IgfaD1QPaA7CHtB7QfEh7IBB4gqNmzEqD9mvZr2q9pvw7br4WzKyOQzqZG70kepA-0HpQOkD0ge0D2gOwB2QOpA-nYw5519ah1sk9kn8g-pW-fLob1I5xdiohQRIQYDGIwMsFgkD0he0r7Ce0ntJ-49xOTO_rm-8ehX-OXHscrCxrw_Lv1ePydOoyefAx3TajFbb-pwb-PqcEPn0_g2meOkmdrkEAcfVOyjitn2g8pw4_wzMDBM6405nQilhdDRIAiRhQxSjeiT-uDIs5kP8h-kP1IjxEdKPYzyNld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rJP_fWfuZGJ_qfP2IXV1HprgLO9nvpaf-LL44-GVwpHl19qvodtGIzpe8fj-2__pXB0dWc3_fnM9v4bVaOdnf75anYvNEJG389WxIsYO2J0eiYiRgxff4mo0vzT_kP7T1-0V-n4pjOmb8Hzz32AUaMW4PbbZ-P-_56OSRPXptPUgP9OphjR5559BgX5S_sI40r2fKDb86Ervo1hK_4R3534tQHA6PZtffXV7GY3wpQdj7__RlBIXqSv2auBIAaT1h-tP1p_tH9GZxBsKdoz4B3ObA1w--79nl_RiJ4PyniIz0jS-iZ9GgjrxVezm6kI2UCPyND4iEHoiwwCrV_KcKAMh74dYab5ufjmJ-fJcSivqsmWPzhgn1vfcAq__PXUnq2xtcqAaL-nDCfCV_0XXzFndxQzFPKSERx6T_IgfaD1QPaA7CHtB7QfEh4gPEB4gPAA4QHCA_0bDwhnV0asnE2N3pM8SB9oPSgdIHtA9oDsAdkDsgdSB8gekj0ke0j2kOxh_7KHwtmliEX_jljQ_NH8EQNFDBQxUMRA0X5OeIbwAOEBwgOEBwgPuPGAK42ZIpYUsaSIJUUsKWLZvyKWNF80X8Q4EuNK-I3wG-E3wm-EB8x4gGp2qUaZarapZp1q9inDhTJ86MwKOrOD8ADhAcIDhAcIDww4PEA1u1SDQzVIVLNONft0ZoGtA8QQEUNEDBExRMQQUcYIZYxQxshAwQP_D1qk899cHOG9AAAAAElFTkSuQmCC
-----------------------------265001916915724
Content-Disposition: form-data; name="filename"

filename.bmp
-----------------------------265001916915724
Content-Disposition: form-data; name="num"

10
-----------------------------265001916915724
Content-Disposition: form-data; name="hl"

de
-----------------------------265001916915724
Content-Disposition: form-data; name="bih"

976
-----------------------------265001916915724
Content-Disposition: form-data; name="biw"

1920
-----------------------------265001916915724--

我完全不知道如何在C#中做到这一点......有什么建议吗?

2 个答案:

答案 0 :(得分:1)

使用c#,您可以使用HttpClient执行Post Multi-part Form Data。

下面的

是我为我尝试过的代码片段。

试一试!!

using (var client = new HttpClient())
{
  //client.DefaultRequestHeaders.Add("User-Agent", "CBS Brightcove API Service");
  string authorization = GenerateBase64();
  client.DefaultRequestHeaders.Add("Authorization", authorization);


  using (var content = new MultipartFormDataContent())
  {

    string fileName = Path.GetFileName(textBox1.Text);

    //Content-Disposition: form-data; name="json"
    var stringContent = new StringContent(InstancePropertyObject);
    stringContent.Headers.Remove("Content-Type");
    stringContent.Headers.Add("Content-Type", "application/json");
    stringContent.Headers.Add("Content-Disposition", "form-data; name=\"instance\"");
    content.Add(stringContent, "instance");

    var fileContent = new ByteArrayContent(filecontent);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
      FileName = fileName
    };
    content.Add(fileContent);

    var result = client.PostAsync(targetURL, content).Result;
    Console.Read();
  }
}

答案 1 :(得分:0)

您可以使用WebClient's upload file method。您可以使用headers property来分配数据包的标头。 WebClient的一些注意事项是:setting timeoutunzipping gzipped responses,最后如果您打算同时发出多个请求removing connections limit