Sto tentando di caricare file multipli utilizzando System.Net.Http.HttpClient.HttpClient: Come caricare più file contemporaneamente
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();
}
questo invia solo mulipart/form-data, ma mi aspettavo multipart/mixed da qualche parte nel post.
AGGIORNAMENTO: Ok, sono andato in giro.
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;
}
Questo sembra corretto su filo squalo. ma non vedo i file nel mio controller.
[HttpPost]
public ActionResult UploadProfileImage(IEnumerable<HttpPostedFileBase> postedFiles)
{
if(postedFiles == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// more code here
}
postedFiles
è ancora nullo. Qualche idea?
Purtroppo, ho in esecuzione in questo troppo: http://stackoverflow.com/questions/15638622/how-to-upload-files-to-asp-net-mvc-4-0-action- running-in-iis-express-with-httpcl/15638623 # 15638623 – deerchao