Trouble uploading photos using server code and the output from the request.body()

I am trying to figure out how to upload a photo using the XHR2 command in server code and using the request.body() to get file data from the app. I am not sure how to convert this string output from this command into something that will properly upload the file to the database. Before sending the photo data from the app to the server code, I have converted the data to a Uint8Array array buffer. The result I get from the request.body() is not a array buffer but a string data that I have not figured out how to convert. I have tried sending it without any changes in the response body for the XHR2.send command results in a file of the incorrect size. This is the server code I am currently using with some lines left out to condense it:


Var databaseId = “”
var fileName = request.get("fileName");
var sessionToken = request.get("sessionToken");
var body = request.body();
var contentType = “image/jpeg”;

try{

var url = "https://api.appery.io/rest/1/db/files/" + fileName;

var params = {
"headers": {
"X-Appery-Database-Id": databaseId,
"X-Appery-Session-Token": sessionToken,
"Content-Type": contentType
},
"body": body,
"isRawRequest": true
};

var XHRResponse = XHR2.send("POST", url, params);
Apperyio.response.success(XHRResponse.body, "application/json");

}catch (error) {
response.error({
"message": "Error Uploading Photo = "
+ error }, 400);
}


The code results in a file in the database that is smaller than the original file by about a kb for 20 kb file when trying to upload the photo from the app. I have tested the server code using Postman also and end up with a smaller file of the same size that will not open when downloaded directly from the database. In addition I have have tested the XHR2.send code using the setup in answer at https://getsatisfaction.com/apperyio/... and the XHR2.send command works using the array from the get function suggested in the linked answer so I am pretty certain the problem with the output from the request.body() command . Does anyone know what I need to do convert the request.body output into something that can be posted to the database and result in photo being properly stored?
1 person has
this question
+1
Reply
  • Hello,

    Please send the binary file data in the body of that request. How do you send it? Please send us screen shots of this problem/implementation so we can better understand what is happening.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • I am currently sending the binary as part of the body both in the app and in my attempts to test the server code using Postman. The mapping in the app looks like this :

    The file string in the mapping is the binary string that is converted from the base64 string from the FileReader. The base64 string is converted to binary using the following code:



    function base64ToBinary(dataURI) {
    //removes the front tag info from the file string and converts the string to binary
    var byteString = atob(dataURI.split(',')[1]);
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
    }
    return ab;
    }


    I have also tested the server code using the API information in Postman using the following setup:



    Both the app uploading and postman result in the same error - a file that that is about 1kb smaller than the same photo file uploaded to directly to the database and the smaller file when downloaded is unreadable. This leads me to believe the error has something to do with whatever is being outputted from var body = request.body() in my previous response.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • In that case, please send the base64 string instead of the binary data to the server code. You can upload it directly to the database, like:
    var fileURL = 'https://api.appery.io/rest/1/db/files/testFile.png';

    var fileUpload = XHR2.send("POST", fileURL, {

    "headers": {
    "X-Appery-Database-Id": "516f4893e4b0e0d8f16ccf9a",
    "X-Appery-Master-Key": "fe03d3be-8769-41a4-b418-872f8f884fa9",
    "Content-Type": "image/png"
    },
    "isRawRequest": true,
    "body": request.body()
    });
    Apperyio.response.success(fileUpload.body, "application/pdf");
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned