Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...