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입니다. 어떤 아이디어?
슬프게도, 나는 너무 이것으로 실행했습니다 http://stackoverflow.com/questions/15638622/how-to-upload-files-to-asp-net-mvc-4-0-action- iis-express-with-httpcl/15638623 # 15638623 – deerchao