2013-06-03 2 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 만 보내지 만 게시물의 멀티 파트/믹스가 예상됩니다.

업데이트 : 확인, 주변에 있습니다.

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 
} 

postedFiles은 여전히 ​​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

Nailed it. 그러나 행동이 이상합니다.

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

관련 문제