HttpClient:如何一次上传多个文件 [英] HttpClient: How to upload multiple files at once

查看:54
本文介绍了HttpClient:如何一次上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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();
}

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

this only sends mulipart/form-data, but i expected multipart/mixed somewhere in the post.

更新:好的,我找到了.

UPDATE: Ok, i got around.

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;
}

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

This looks correct on wire shark. but i do not see the files in my controller.

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

    // more code here
}

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

postedFiles is still null. Any ideas?

推荐答案

搞定了.但是行为很奇怪.

Nailed it. But behaviour is strange.

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
}

这篇关于HttpClient:如何一次上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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