【问题标题】:HttpClient: How to upload multiple files at onceHttpClient:如何一次上传多个文件
【发布时间】:2013-06-03 22:13:13
【问题描述】:

我正在尝试使用System.Net.Http.HttpClient 上传多个文件。

using (var content = new MultipartFormDataContent())
{
   content.Add(new StreamContent(imageStream), "image", "image.jpg");
   content.Add(new StreamContent(signatureStream), "signature", "image.jpg.sig");

   var response = await httpClient.PostAsync(_profileImageUploadUri, content);
   response.EnsureSuccessStatusCode();
}

这只会发送多部分/表单数据,但我希望多部分/混合在帖子的某个地方。

更新:好的,我知道了。

using (var content = new MultipartFormDataContent())
{
    var mixed = new MultipartContent("mixed")
    {
        CreateFileContent(imageStream, "image.jpg", "image/jpeg"),
        CreateFileContent(signatureStream, "image.jpg.sig", "application/octet-stream")
    };

    content.Add(mixed, "files");

    var response = await httpClient.PostAsync(_profileImageUploadUri, content);
    response.EnsureSuccessStatusCode();
}

private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
    var fileContent = new StreamContent(stream);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("file") {FileName = fileName};
    fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
    return fileContent;
}

这在钢丝鲨上看起来是正确的。但我看不到控制器中的文件。

[HttpPost]
public ActionResult UploadProfileImage(IEnumerable<HttpPostedFileBase> postedFiles)
{
    if(postedFiles == null)
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

    // more code here
}

postedFiles 仍然为空。有什么想法吗?

【问题讨论】:

标签: c# file-upload .net-4.5 dotnet-httpclient


【解决方案1】:

成功了。但是行为很奇怪。

using (var content = new MultipartFormDataContent())
{
    content.Add(CreateFileContent(imageStream, "image.jpg", "image/jpeg"));
    content.Add(CreateFileContent(signatureStream, "image.jpg.sig", "application/octet-stream"));

    var response = await httpClient.PostAsync(_profileImageUploadUri, content);
    response.EnsureSuccessStatusCode();
}

private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
    var fileContent = new StreamContent(stream);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
    { 
        Name = "\"files\"", 
        FileName = "\"" + fileName + "\""
    }; // the extra quotes are key here
    fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);            
    return fileContent;
}

[HttpPost]
public ActionResult UploadProfileImage(IList<HttpPostedFileBase> files)
{
    if(files == null || files.Count != 2)
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

    // more code
}

【讨论】:

  • 哇,你真是个天才。完美运行!
  • @KirkWoll 谢谢。实际上,我最近在使用 Xamarin 时遇到了一个问题,因为在添加额外引号时它会失败。所以我不得不编写自己的 MultipartFormDataContent 类
  • @esskar 服务器端代码是什么样的?我无法让它工作。
  • 为了将来的参考,如果有人不能让它在其他后端服务器(如 Laravel 或 Express)中工作,只需将数组 [] 添加到 Name = "\"files\"'' => Name = "\"files[]\"'' 我卡住了这个问题花了几个小时才得到解决。
  • whats Stream stream FileStream,MemoryStream,什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多