我正在开发一个项目,我必须在AngularJS中使用文件上传一小部分JSON.
我使用Danial Farid的角度文件上传编写代码,它正在工作,除了它总是发送"multipart/form-data,boundary = <whatever>"
但是,我必须使用multipart/mixed.
这是我的电话:
$scope.upload = $upload.upload({
url: <my url>,
method: 'POST',
data: $scope.data,
file: file,
}).progress(function(evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function(data, status, headers, config) {
// file is uploaded successfully
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
有没有办法在发送之前修改标头?
如果没有使用他的角度文件上传,那么通过另一种方法,希望没有"滚动我自己的"功能?
编辑1:
我只是无法理解如何做出这种改变是如此困难.当然可以添加
headers: {'Content-Type': 'multipart/mixed'}
Run Code Online (Sandbox Code Playgroud)
但这绝对没有,因为没有边界.为什么没有办法拉出边界?就像是
headers: {'Content-Type': 'multipart/mixed, boundary=%b'}
Run Code Online (Sandbox Code Playgroud)
我需要尽快完成这项工作.
我有一个奇怪的(对我来说至少),希望有人能够提供帮助.也许我只是在考虑这个错误的方式或者其他什么.尝试使用javascript中的文件对我来说是全新的.
我有一个指令,其模板如下所示:
<input type="file" ng-file-select ng-model="files">
<button ng-click="onFileSelect()">Upload</button>
Run Code Online (Sandbox Code Playgroud)
在指令的控制器中,我这样做:
$scope.onFileSelect = function() {
reader = new FileReader();
reader.onload = function() {
$scope.file_contents = this.result;
};
reader.readAsArrayBuffer($scope.files[0]);
//debugger;
$scope.$watch('file_contents',function(file_contents) {
console.log(file_contents);
if (angular.isDefined(file_contents)) {
... bunch of code attempting to deal with file_contents ...
}
});
};
Run Code Online (Sandbox Code Playgroud)
我选择一个文件,然后单击"上传"按钮.(file_contents)的console.log未定义.只有当我第二次点击按钮时,它才有值.
如果我取消注释调试器,$ scope.file_contents在我检查它时会有一个值.
因此,file_contents需要一些时间来设置(这是预期的),但$ watch似乎从未注意到?这有什么奇怪的原因吗?$ watch无法使用FileReader对象吗?
编辑1:
好的.这是一些更多的信息.根据PSL的建议,我现在有了这样的代码:
$scope.onFileSelect = function() {
reader = new FileReader();
reader.onload = function() {
file_contents = this.result;
upload_file($scope.files[0],file_contents);
};
reader.readAsArrayBuffer($scope.files[0]);
};
upload_file = function(file,file_contents) {
<lots of cool …Run Code Online (Sandbox Code Playgroud) 我有一个项目要添加模式。太好了,所以我读了起来,听起来好像要使用$ dialog。我已经有角度了,我有bootstrap和bootstrap-ui。
阅读2013年的一篇文章时,他们说:“嘿,去获取Angular-UI模块,这就是$ dialog所在的位置!”
好的,我去了angular-ui网站,除非我真的很愚蠢(也许我是),否则没有Angular-UI Module这样的东西。我也不能轻易找出那个站点上的哪个文件(因为有一个完整的文件束)包含神奇的$ dialog。
救命?!
其中包括:
<script src="lib/AngularJS/angular.js"></script>
<script src="lib/AngularJS/angular-route.js"></script>
<script src="lib/AngularJS/angular-sanitize.js"></script>
<script src="lib/jQuery/jquery-2.1.1.js"></script>
<script src="lib/bootstrap/js/bootstrap.js"></script>
<script src="lib/bootstrap-ui/ui-bootstrap-tpls-0.12.0.js"></script>
<script src="lib/lodash/lodash.min.js"></script>
<script src="lib/angular-file-upload/dist/angular-file-upload-all.js"></script>
var myApp = angular.module("myApp",
['ngRoute',
'angularFileUpload',
'ui.bootstrap.tpls',
'ui.bootstrap.rating',
'ui.bootstrap',
'ngMap',
'ngSanitize']
);
myApp.controller('myController',
['$scope','$rootScope', '$dialog',
function ($scope, $rootScope, $dialog) {
Run Code Online (Sandbox Code Playgroud)
如果我从控制器中删除$ dialog,一切都很好。一旦将其放入其中,就会收到未知的提供程序错误。