上传与FORMDATA使用AngularJS和Asp.Net MVC多个文件 [英] Upload multiple files with FormData using AngularJS and Asp.Net MVC
本文介绍了上传与FORMDATA使用AngularJS和Asp.Net MVC多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要上传采用了棱角分明的js多个文件,对于这个我使用FORMDATA()。
I want to upload multiple files using angular js, for this I am using FormData() .
这里是我的表单字段
<input name="profileImage" type="file" class="upload"
onchange="angular.element(this).scope().LoadFileData(this.files)" accept="image/*">
<input name="avatarImage" type="file" class="upload"
onchange="angular.element(this).scope().LoadFileData(this.files)" accept="image/*">
<input name="Id" type="text" ng-model="user.Id" />
<input name="Name" type="text" ng-model="user.Name" />
这是我的Asp.Net MVC控制器
public ActionResult AddUser(string user, HttpPostedFileBase[] files)
{
// parse user into User
}
和角控制器
.controller('formController',['$scope','$http', function ($scope, $http) {
$scope.user = {
Id: 0,
Name: ""
};
$scope.files = [];
$scope.LoadFileData = function(files) {
$scope.files = files;
};
$scope.submit = function() {
$http({
url: "/Home/AddUser",
method: "POST",
headers: { "Content-Type": undefined },
transformRequest: function(data) {
var formData = new FormData();
formData.append("user", angular.toJson(data.user));
for (var i = 0; i < data.files.length; i++) {
formData.append("files[" + i + "]", data.files[i]);
}
return formData;
},
data: { user: $scope.user, files: $scope.files }
})
.success(function(response) { });
};
});
问题是,我要上传多张图片一个配置文件,另一个用于化身。当我同时上传只有第二个显示了其覆盖的previous之一。我试着把$ scope.files.push(文件),但它给null.Need帮助我所缺少的在那里。
Problem is that I want to upload more than one image one for profile and the other for avatar . When I upload both only the second one shows up which override the previous one . I tried to push $scope.files.push(files) but it gives null.Need help what I am missing there.
推荐答案
更改 LoadFileData code>函数
$scope.LoadFileData = function (files) {
$scope.files.push(files[0]);
};
文件
返回一个文件清单对象和实际的文件对象是位于索引0
files
returns a Filelist object and actual file object is at index 0.
这篇关于上传与FORMDATA使用AngularJS和Asp.Net MVC多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文