我使用输入类型文件和其他用户数据一起上传图像。我的模型由用户类:使用角度js发布FormData文件以及模型数据
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Rollno { get; set; }
public byte[] ProfileImage { get; set; }
public HttpPostedFile image { get; set; }
}
和在asp.net mvc的一个发布方法:
public ActionResult AddUser(User user)
{
var files = Request.Files;
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
byte[] uploadFile = new byte[file.InputStream.Length];
}
return View();
}
我有一个输入标签是这样的:
<input id="imgInp" type="file" aria-label="Add photos to your post" class="upload" name="file" onchange="angular.element(this).scope().LoadFileData(this.files)" multiple="" accept="image/*">
和angularJS这样的控制器:
angular.module('app', ['AngularDemo.BeerController']).
controller('formController',['$scope','$http', function ($scope,$http) {
alert('in controller');
var formData = new FormData();
$scope.LoadFileData = function (files) {
for (var file in files) {
formData.append("file", files[file]);
}
};
$scope.submit = function() {
alert('in submit');
$http.post("/Home/AddUser", formData, {
withCredentials: true,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
}).success(function (response) {
});
}
}]);
我的图像文件回传到AddUser
方法中,但是如何发送模型数据?我在表格中添加了ng-model="Name"
等。如何将$scope.Name
等与该图像数据一起发送,因为我的$http.post
将表单数据视为传递数据?
你岩石的人....这工作 –
你有模型填充? – Sana