2013-06-03 16 views
18

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?

+0

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

risposta

22

Inchiodato. Ma il comportamento è strano.

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

Wow, sei un genio. Ha funzionato perfettamente! –

+0

@KirkWoll ringrazia. Recentemente ho avuto un problema con Xamarin, dato che fallisce inserendo le virgolette extra. Quindi ho dovuto scrivere la mia classe MultipartFormDataContent – esskar

+0

@esskar come appare il codice lato server? Non riesco a farlo funzionare. – guiomie

Problemi correlati