我想将Ionic应用程序中的图片发送到Laravel后端,但我一直在应用中获得FileTransferError.FILE_NOT_FOUND_ERR (Code=1)
。
拍照:
$scope.openCamera = function () {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: 1, // 0:Photo Library, 1=Camera, 2=Saved Photo Album
encodingType: 0 // 0=JPG 1=PNG
});
function onSuccess(FILE_URI) {
$scope.picData = FILE_URI;
$scope.$apply();
}
function onFail(message) {
alert('Error (' + message + ')');
}
}
}
但发送不断抛出错误:
$scope.send = function () {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var myImg = $scope.picData;
var options = new FileUploadOptions();
options.fileKey = "post";
options.chunkedMode = false;
options.fileName = 'someFileName.jpg';
options.mimeType = "image/jpeg";
var params = {};
params.token = localStorage.getItem('token');
options.params = params;
var ft = new FileTransfer();
ft.upload(myImg, encodeURI(SERVER + 'user/post'), win, fail, options);
}
}
我注意到,当我查看error.source
时,我无法找到列出的文件。我无法在浏览器上调试(缺少相机),因此我将所有内容部署到手机上。
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
//alert("upload error target " + error.target);
}
答案 0 :(得分:0)
var headers = {'Authorization':'Bearer '+ $localStorage.get('token')};
options.headers = headers;