0

비슷한 질문을 여러 번 시도했지만 명확한 답변이 없지만 여전히 문제가 있습니다. HttpPostedFileBase is null - AngularJS에서 MVC로 파일 올리기

public class SubmitModel 
{ 
    public string Name { get; set; } 
    public HttpPostedFileBase File { get; set; } 
    public IEnumerable<HttpPostedFileBase> Files { get; set; } 
} 

이것은 MVC 코드

[HttpPost] 
public ActionResult Test(SubmitModel model) 
{ 
    // Here model.File and model.Files is always null 
} 

입니다 이것은 내가 AngularJS와 당신이 알고 싶은 경우에

var data = { 
    name: scope.name,  // This is passed to MVC successfully 
    file: scope.files[0], // Doesn't even work with single file 
    files: scope.files // This is a FileList 
}; 
$http.post("/umbraco/surface/MyController/Test", data).success(...); 

을 사용하여 제출 무엇 C#에서 모델이 얼마나 I 할당 scope.files :

$('#upload').on('change', function (e) { 
    scope.$apply(function() { 
     scope.files = e.target.files; 
    }); 
}); 

내가 누락 된 부분을 볼 수 있습니까?

답변

1

해결했습니다!

그것이 MVC에서 다음
var data = new FormData(); 
angular.forEach(scope.item, function (value, key) { 
    if (key == "files") { 
     for (var i = 0; i < value.length; i++) { 
      data.append(value[i].name, value[i]); // Filename:File 
     } 
    } else { 
     data.append(key, value); 
    } 
}); 

$http.post("/umbraco/surface/MyController/Test", data, { 
       transformRequest: angular.identity, 
       headers: { 'Content-Type': undefined } 
      }).success(...); 

를 제출하는 방법, 우리가 Request.Files에서 파일을 얻을, 그것은 모델에되지 않습니다.

[HttpPost] 
public ActionResult Test(SubmitModel model) 
{ 
    var files = Request.Files; // a collection of HttpPostedFileBase 
    Save(model, files); 
} 

상세 정보 :
https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
http://www.codeproject.com/Tips/878730/File-Upload-Using-AngularJS-and-ASP-NET-MVC