我正在使用ng-file-upload来上传图片文件。上传按钮的html代码是
<button ng-disabled="croppedDataUrl===''" class="button button-3d button-rounded" ng-click="vm.upload(croppedDataUrl)">Upload</button>
并且控制器的代码如下
vm.upload = function(dataUrl) {
Upload.upload({
url: AppConstants.api.images,
data: {
uploadedPicture: dataUrl,
uploadedFrom: 'recipe'
},
}).then(function(response) {
$timeout(function() {
$scope.result = response.data;
});
}, function(response) {
if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data;
}, function(evt) {
$scope.progress = parseInt(100.0 * evt.loaded / evt.total);
});
}
当我点击上传按钮时,会抛出以下错误..
undefined不是一个函数 在Upload.upload
我在控制器中注入了上传服务 并且包含的js文件的顺序如下
angular.js
NG-文件上传-shim.js
NG-文件upload.js
所以,我不知道问题出在哪里。提前谢谢....
编辑堆栈跟踪
TypeError: undefined is not a function
at vm.upload (http://localhost:8888/yn-site/app/dist/js/app.min.js:5000:24)
at fn (eval at <anonymous> (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:13365:15), <anonymous>:4:379)
at callback (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:23613:17)
at Scope.$eval (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:16052:28)
at Scope.$apply (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:16152:25)
at HTMLButtonElement.<anonymous> (http://localhost:8888/yn-site/app/dist/js/vendor.min.js:23618:23)
at HTMLButtonElement.m.event.dispatch (http://localhost:8888/yn-site/js/jquery.js:2:40587)
at HTMLButtonElement.r.handle (http://localhost:8888/yn-site/js/jquery.js:2:37297) vendor.min.js:12520(anonymous function) vendor.min.js:12520(anonymous function) vendor.min.js:9292Scope.$apply vendor.min.js:16157(anonymous function) vendor.min.js:23618m.event.dispatch jquery.js:2r.handle
答案 0 :(得分:4)
好像你没有正确初始化/注入依赖项。
在模块中:
var app = angular.module('your_module', ['ngFileUpload']);
在控制器中:
app.controller('MyCtrl', ['$scope', 'Upload', function ($scope, Upload) {
$scope.upload = function(dataUrl) {
Upload.upload({
url: AppConstants.api.images,
data: {
uploadedPicture: dataUrl,
uploadedFrom: 'recipe'
},
}).then(function(response) {
$timeout(function() {
$scope.result = response.data;
});
}, function(response) {
if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data;
}, function(evt) {
$scope.progress = parseInt(100.0 * evt.loaded / evt.total);
});
}
}
答案 1 :(得分:0)
如果您想购买vanilla js,这段代码将适用于任何库或没有库并兼容ng-file-upload -
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
var formData = new FormData;
formData.append('YOUR_FILE_OBJECT_NAME', file);
xhr.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "YOUR_URL_HERE");
xhr.send(formData);