我有 Angular 服务,它在移动应用程序中执行 Cordova 文件传输上传 - 它将文件上传到媒体服务器并返回一个对象。我试图将此对象传递给该media_response
属性,但我没有运气 - 我做错了什么?
请注意 - 我已经尝试使用service
andfactory
并且似乎仍然没有得到任何乐趣 -$cordovaFileTransfer upload
由于某些奇怪的原因,块内的任何内容都没有传递给类属性。
我希望看到media_response
与数据变量相同的输出,数据变量是从媒体服务器返回的对象,但它说no response
知道为什么我没有得到预期的响应吗?
// 预期响应
UploadService.media_response in the controller should be an object to match the console.log(data)
// 实际响应
UploadService.media_response is the string 'no response'
// 上传服务
abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {
var UploadService = function() {
this.upload_in_progress = false;
this.progress = 0;
this.media_response = '';
};
UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
this.upload_in_progress = true;
this.media_response = 'no response';
var options = {
id: new Date() . getTime() + filename,
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params : {'fileName': filename}
};
$cordovaFileTransfer.upload(url, targetPath, options).then(
function(result) {
// Success!
var data = JSON.parse(result.response);
console.log('file uploaded - response:', data); // ALWAYS A OBJECT
this.media_response = 'fake response2';
UploadService.media_response = 'fake response3';
if (angular.isDefined(data.id)) {
angular.forEach(data, function(value, key) {
// image[key] = value;
});
// $scope.post.linkThumbnail = false;
// $scope.post.video = '';
} else if (angular.isDefined(data.message)) {
// $scope.uploadError = data.message;
} else {
}
}, function(err) {
}, function (progress) {
// constant progress updates
this.progress = parseInt(100 * progress.loaded / progress.total) + '%';
}
);
};
return new UploadService();});
// 控制器
UploadService.cordovaFileUpload(filename, url, targetPath, options);
console.log(UploadService); // to view in js console