2013-06-03 65 views
18

System.Net.Http.HttpClientを使用して複数のファイルをアップロードしようとしています。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(); 
} 

これはmulipart/form-dataを送信するだけですが、投稿内のどこかでmultipart/mixedが必要です。

更新:よろしくお願いします。

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 
} 

はまだnullです。何か案は?

+0

悲しいことに、私もこのに実行してきました:http://stackoverflow.com/questions/15638622/how-to-upload-files-to-asp-net-mvc-4-0-action- iis-express-with-httpcl/15638623#15638623 – deerchao

答えて

22

釘付け。しかし、行動は変です。

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 
} 
+0

うわー、あなたは天才です。完璧に働いた! –

+0

@KirkWollありがとうございます。私は実際にXamarinを使って問題に直面しました。余分な引用符を入れると失敗します。だから私は自分のMultipartFormDataContentクラスを書く必要があった – esskar

+0

@esskarどのようなサーバー側のコードのように見えるのですか?私はこれを働かせることはできません。 – guiomie

関連する問題