Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

c# - HttpClient: How to upload multiple files at once

I am trying to upload muliple files using 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 is still null. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...