Javascript 角度文件上传在更改后立即发生
我在通过angular上传文件时遇到了一些困难,并决定采用以下解决方案:Javascript 角度文件上传在更改后立即发生,javascript,angularjs,Javascript,Angularjs,我在通过angular上传文件时遇到了一些困难,并决定采用以下解决方案: $scope.uploadFile = function(files) { var fd = new FormData(); //Take the first selected file fd.append("file", files[0]); console.log($scope.files); $http.post('../resourc
$scope.uploadFile = function(files) {
var fd = new FormData();
//Take the first selected file
fd.append("file", files[0]);
console.log($scope.files);
$http.post('../resource/editor', fd, {
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).then(function(data){
//this keeps the user from having to refresh the page
DataContext.getEditors().then(function(data){
$scope.myData = data;
});
}).catch(function(reason){
});
};
<form name="myForm" >
<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
<input type="submit" ng-click="uploadFile()" value='Submit'/>
</form>
$scope.uploadFile=函数(文件){
var fd=新FormData();
//获取第一个选定的文件
fd.append(“文件”,文件[0]);
log($scope.files);
$http.post('../resource/editor',fd{
事实上,
标题:{'Content-Type':未定义},
转换请求:angular.identity
}).then(功能(数据){
//这样用户就不必刷新页面
DataContext.getEditors().then(函数(数据){
$scope.myData=数据;
});
}).catch(函数(原因){
});
};
当前的行为是,只要用户选择一个文件,该文件就开始上传。我的意图是,在按钮的点击事件中,用户将上传文件。完成此任务我缺少什么?当文件输入发生更改时,您似乎正在调用
uploadFile()
。这就是为什么它会立即发生。如果需要知道选择了哪些文件,请添加一条语句来更新某些模型数据onchange
:
<input type="file" name="file" onchange="$scope.files = this.files"/>
希望这有帮助 如果您在更改输入时调用upload file,我会给输入一个模态,并将该值传递给单击按钮的上载文件
<form name="myForm" >
<input type="file" name="file" ng-model="fileNames" />
<input type="submit" ng-click="uploadFile(fileName)" value='Submit'/>
</form>
<form name="myForm" >
<input type="file" name="file" ng-model="fileNames" />
<input type="submit" ng-click="uploadFile(fileName)" value='Submit'/>
</form>