Hi,
I'm trying to upload an image file to the DB file collection. What i do is: Select an image from the phone gallery using Camera service and upload it.
I've used the following topic for help:
https://getsatisfaction.com/apperyio/...
My upload script is the following:
precode
function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia = byteString.charCodeAt(i);
}
return new Blob([ab], {type: 'image/jpeg'});
}
function upload(dbBase64) {
// Generate a unique name
var userId = localStorage.getItem('userId');
var dishName = localStorage.getItem('toAddDishName');
var fileName = dishName + userId;
Code: Select all
var serverUrl = 'https://api.appery.io/rest/1/db/files/' + fileName;
var f = dataURItoBlob(dbBase64);
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Appery-Database-Id", Testeo_settings['database_id']);
request.setRequestHeader("X-Appery-Session-Token", localStorage.getItem('userToken'));
request.setRequestHeader("Content-Type", 'image/jpeg');
},
url: serverUrl,
data: f,
processData: false,
contentType: false,
success: function(data) {
// We uploaded the image to the server. Now put it in the table.
var uploadedFileName = data.filename;
localStorage.setItem('toAddImageFileName', uploadedFileName);
},
error: function(xhr, textStatus, error) {
alert("error");
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
}); }
/code/pre
the codedbBase64/code variable is the Base64 data that i get back from the camera service.
When i try to upload the image i get "Bad request" error
Am i doing something wrong?
I've also seen that you've updated the file upload tutorial but i thing i cannot use the input method as my data has to come from the phone gallery