$cordovaFileTransfer.upload [solved]


#1

You can upload multiple files with $ cordovaFileTransfer.upload?


#2

Google is your friend, look at the upload function below.


#3

If you’re developing for Blackberry then you’ll need to make sure that you wait for $cordovaFileTransfer.upload to finish uploading the file before calling it again otherwise it triggers an InvalidStateError: DOM Exception 11 error.

The following is an example function that queues the files and uploads them one at a time.

function uploadFiles(server, filePaths, options) {
	var q = $q.defer();
	function processUpload(fileUploadResult) {
		var filePath = filePaths.pop();
		if (filePath) {
			console.log('uploadFiles: uploading ' + filePath);
			$cordovaFileTransfer.upload(server, filePath, options(filePath)).then(processUpload, q.reject);
		}
		else {
			q.resolve({ success: true });
		}
	}
	console.log('uploadFiles: ' + filePaths.length + ' files queued');
	processUpload();
	return q.promise;
}

Then you can just do something like the following.

var filePaths = [
	'files:///file1', 
	'files:///file2'
];

uploadFiles('http://server.dev/upload', filePaths, function(filePath) {
	return {
		fileKey: 'file',
		params: {
			filePath: filePath
		},
		chunkedMode: false
	};
});

#4

Thank you very much!
It was a great help!