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

関連する問題