2013-06-03 33 views
18

Tôi đang cố tải lên các tệp muliple bằng cách sử dụng System.Net.Http.HttpClient.HttpClient: Cách tải lên nhiều tệp cùng một lúc

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(); 
} 

điều này chỉ gửi dữ liệu mulipart/form, nhưng tôi dự kiến ​​sẽ trộn/trộn ở đâu đó trong bài đăng.

CẬP NHẬT: Ok, tôi đã tham gia.

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; 
} 

Điều này có vẻ đúng trên dây cá mập. nhưng tôi không thấy các tập tin trong bộ điều khiển của tôi.

[HttpPost] 
public ActionResult UploadProfileImage(IEnumerable<HttpPostedFileBase> postedFiles) 
{ 
    if(postedFiles == null) 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 

    // more code here 
} 

postedFiles vẫn là giá trị rỗng. Ý tưởng nào?

+0

Đáng buồn thay, tôi cũng đã tham gia vào quá trình này: 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

Trả lời

22

Đóng đinh nó. Nhưng hành vi là lạ.

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

Chà, bạn là một thiên tài. Làm việc hoàn hảo! –

+0

@KirkWoll cảm ơn. Tôi thực sự gặp phải vấn đề gần đây khi sử dụng Xamarin, vì nó không thành công khi đưa vào các dấu ngoặc kép thêm. Vì vậy, tôi đã phải viết lớp MultipartFormDataContent của riêng mình – esskar

+0

@esskar mã phía máy chủ trông như thế nào? Tôi không thể làm việc này được. – guiomie

Các vấn đề liên quan