Upload image using HTTP Post

You must Login before you can answer or comment on any questions.

I am trying to upload the picture that I take from camera to a rest service using HTTP Post method. I want to send the image as a blob. However when I use the following code to upload image,the image that gets on server is either empty or corrupted. Can someone please take a look at this code to see if this is the right way of sending blob over HTTP:

function openCamera(win) {
        //open camera
        if (Titanium.Media.isCameraSupported) {
            Titanium.Media.showCamera({
                success : function(e) {
 
                    win.ImageView.setImage(e.media);
 
                    var bl = win.ImageView.toImage();
                    addPhoto(bl);
                    //addPhoto('../images/GPS.png');
 
                },
                error : function(e) {
                    Ti.UI.createAlertDialog({
                        title : 'Error',
                        message : 'There was an error in taking picture'
                    }).show();
 
                },
                cancel : function(e) {
 
                },
                allowEditing : true,
                saveToPhotoGallery : false,
                mediaTypes : [Titanium.Media.MEDIA_TYPE_PHOTO]
            })
 
        }
    }
 
function addPhoto(image) {
 
        var format = 'json';
 
        xhr = Titanium.Network.createHTTPClient();
 
        xhr.timeout = 60000;
 
        xhr.onload = function() {
 
        };
 
        var url = 'https://api.parse.com/1/files/pic7.jpg';
 
        xhr.open('POST', url);
 
        xhr.setRequestHeader('X-Parse-Application-Id', 'Q6EX');
 
        xhr.setRequestHeader('X-Parse-REST-API-Key', 'yJ8C617');
 
        xhr.setRequestHeader("Content-Type", "image/jpeg");
 
        xhr.send(image);
 
    }
Thanks

1 Answer

I'm not really sure, but shouldn't you send your imagedata as a param of the POST request?

Like so: xhr.send({imgData: image});

— answered 12 months ago by Tobias Reike
answer permalink
5 Comments
  • Just saw the content-type image/jpeg, so I think you don't need the params format I proposed. So does the server automatically convert the png to jpeg?

    — commented 12 months ago by Tobias Reike

  • I am sending JPEG and not png and hence the content-type format. However I am not sure if the image data is being sent as binary data or not?

    — commented 12 months ago by Apurva Goyal

  • Shoud I be encoding the image before posting it?

    — commented 12 months ago by Apurva Goyal

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!