HttpClient: So laden Sie mehrere Dateien gleichzeitig hoch

Ich versuche, mehrere Dateien mit hochzuladenSystem.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();
}

dies sendet nur mulipart / formulardaten, aber ich habe erwartet, dass irgendwo in der post multipart / mixed ist.

UPDATE: Ok, ich bin rumgekommen.

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

Dies sieht bei Drahthaien richtig aus. aber ich sehe die dateien nicht in meinem controller.

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

    // more code here
}

postedFiles ist immer noch null. Irgendwelche Ideen?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage