Page 1 of 2

Upload image from phone gallery

Posted: Sat May 10, 2014 2:10 pm
by Slava Balabanov

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&#46;length; i++) {
ia = byteString&#46;charCodeAt(i);
}
return new Blob([ab], {type: 'image/jpeg'});
}
function upload(dbBase64) {
&#47;&#47; Generate a unique name
var userId = localStorage&#46;getItem('userId');
var dishName = localStorage&#46;getItem('toAddDishName');
var fileName = dishName + userId;

Code: Select all

 var serverUrl = 'https:&#47;&#47;api&#46;appery&#46;io/rest/1/db/files/' + fileName; 
 var f = dataURItoBlob(dbBase64); 

 $&#46;ajax({ 
 type: "POST", 
             beforeSend: function(request) { 
                 request&#46;setRequestHeader("X-Appery-Database-Id", Testeo_settings['database_id']); 
                 request&#46;setRequestHeader("X-Appery-Session-Token", localStorage&#46;getItem('userToken')); 
                 request&#46;setRequestHeader("Content-Type", 'image/jpeg'); 
             }, 
             url: serverUrl, 
             data: f, 
             processData: false, 
             contentType: false, 
             success: function(data) { 
                 &#47;&#47; We uploaded the image to the server&#46; Now put it in the table&#46; 
                 var uploadedFileName = data&#46;filename;                     

                 localStorage&#46;setItem('toAddImageFileName', uploadedFileName); 
             }, 
 error: function(xhr, textStatus, error) { 
  alert("error"); 
                 console&#46;log(xhr&#46;statusText); 
  console&#46;log(textStatus); 
  console&#46;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


Upload image from phone gallery

Posted: Sun May 11, 2014 1:43 am
by Igor

Hello,

Could you also attach screen shot with error?


Upload image from phone gallery

Posted: Sun May 11, 2014 6:09 am
by Slava Balabanov

I print all the variables on the error event (the status text, error).. It just says "bad request".. Nothing else, so I don't really know what to look for. Maybe the xhr object can contain some other info?
But I actually think that it has something to do with the image data because when I do the same with an input tag (as shown in the other thread that I took reference from) then it works


Upload image from phone gallery

Posted: Sun May 11, 2014 7:13 am
by Illya Stepanov

Hi Slava, what device are you using? Have you tried to debug it on the device?


Upload image from phone gallery

Posted: Sun May 11, 2014 7:21 am
by Slava Balabanov

This is the message that i get when using the device, because i can only use the camera service on the device. I look at the console through the Weinre.
Using Nexus 5


Upload image from phone gallery

Posted: Tue May 13, 2014 10:18 am
by Kateryna Grynko

Hi Slava,

Could you please share your app with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a and tell us its name?


Upload image from phone gallery

Posted: Tue May 13, 2014 10:33 am
by Slava Balabanov

Hi,
Its already shared. Its called Plate-O.
The is relevant for the upload is uploadImage if I recall and the page is AddDish


Upload image from phone gallery

Posted: Tue May 13, 2014 1:49 pm
by Kateryna Grynko

Hi Slava,

What are your test credentials?


Upload image from phone gallery

Posted: Tue May 13, 2014 2:11 pm
by Slava Balabanov

The process is: login with user 't@t' and password '123'
Press Add Dish on the right of the footer.
Choose anything from the lists 2 times
On the device,press the picture box and choose gallery.
Pick an image and then press approve button on right of the header.
For the PC browser it's the same only there is a pick image button and upload PC button


Upload image from phone gallery

Posted: Tue May 13, 2014 2:17 pm
by Evgene Karachevtsev

Hello Slava,

Thank you for information. We will test it, but testing might take some time.