Reputation: 2410
I am using cordova file transfer to upload an image to server. The problem is cordova file transfer always throwing error code 1. It worked before suddenly today it stop working.
I dont see any param options value received on server. My code
savebase64AsImageFile(folderpath,filename,realData,dataType,q).then(function (success){
console.log(success);
$cordovaFile.checkFile(folderpath, filename).then(function (success) {
console.log(success);
success.getMetadata(function(metadata) {
console.log('meta size2 ' +metadata.size);
});
}, function (error) {
});
$scope.data.image = filename;
$cordovaFileTransfer.upload(encodeURI(server), filePath, options, true).then(function(result) {...
I already checked using $cordovaFile.checkFile to make sure the file exists. and the options
var options ={
fileName: filename,
chunkedMode: true,
params:{'user_id' : $scope.data.userid},
headers:{Connection: 'close'}
};
On my server I am using Laravel but the $request data is empty, I cant access the file or the user_id.
For the folderpath I am using this
var folderpath = cordova.file.dataDirectory;
var filePath = folderpath + filename;
filepath will return file location
file:///data/user/0/com.ionicframework.test3829076/files/PV5xo1506004208779.jpg
I am testing this on android device
status 409 is number I am giving if the request is null
Any idea what cause this error? This part of code always worked before.
Upvotes: 0
Views: 6350
Reputation: 26
Code 1 can be the access issue. In my case there was a problem with file permissions. If you use sdk version less then 29 (android 10) you have to force user to give read permission
add cordova-plugin-android-permissions in config.xml
const filesPermissions = (permissions) => [
permissions.READ_EXTERNAL_STORAGE,
permissions.WRITE_EXTERNAL_STORAGE,
];
const checkFilesPermissions = async () => {
const { permissions } = window.cordova.plugins;
return Promise.all(filesPermissions(permissions).map(permission => {
return new Promise((resolve, reject) => permissions.checkPermission(permission, resolve, reject));
})).then(statuses => statuses.every(status => status.hasPermission));
};
const requestFilesPermissions = async () => {
const { permissions } = window.cordova.plugins;
return new Promise((resolve, reject) => {
permissions.requestPermissions(filesPermissions(permissions), resolve, reject);
}).then(status => status.hasPermission);
};
// onClickHandler:
if (deviceService.isAndroidPlatform()) {
try {
let hasFilesPermissions = await checkFilesPermissions();
if (!hasFilesPermissions) {
hasFilesPermissions = await requestFilesPermissions();
}
if (!hasFilesPermissions) {
throw new Error('There is no permissions to download files. Please enable it in system preferences');
}
} catch (error) {
alert(error.message)
}
}
// and then just upload your file using cordova-plugin-file-transfer as usual
Note: you have to enable READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in your AndroidManifest.xml
Upvotes: 0
Reputation: 1554
Try uploading without the options or use
var options ={
fileName: filename,
chunkedMode: false,
params:{'user_id' : $scope.data.userid},
headers:{Connection: 'close'}
};
Upvotes: 0
Reputation: 1110
According to the docs, you have your parameters backwards. it's upload(filepath, server, etc.)
https://www.npmjs.com/package/cordova-plugin-file-transfer
Upvotes: 0