Sample code to upload an image

696 views
Skip to first unread message

Aerik Sylvan

unread,
Sep 2, 2014, 8:26:15 PM9/2/14
to androi...@googlegroups.com
Here's a function to upload an image to deviantsart.com (just as an example; they have a *very* simple interface) - I used this, for example, with the sample camera apps... anybody got anything better?

function uploadImage(filename){
    //load the file into a blob
    var oReq = new XMLHttpRequest();
    oReq.open("GET", "file://"+filename, true);
    oReq.responseType = "arraybuffer";

    oReq.onload = function(oEvent) {
        var blob = new Blob([oReq.response], {type: "image/jpg"});
        Log("loaded");
        var client = new XMLHttpRequest();
        var formData = new FormData();
        /* Add the file */ 
        formData.append("file", blob,"myfile.jpg");
        client.onload = function () {
            if (client.status === 200) {
                //for deviantsart, this is the url of the image
                Log(client.responseText);
            }  
        };
        client.open("POST", "http://deviantsart.com", true);
        client.send(formData);  /* Send to server */ 
    };
    oReq.send();
    return;
}



Jorge Ramirez

unread,
Sep 3, 2014, 4:45:27 PM9/3/14
to androi...@googlegroups.com
Great!

I posted a link to this sample to my androidscript subreddit reddit.com/r/androidscript for future reference, thanks!

salvatore fusto

unread,
Oct 3, 2014, 11:30:35 AM10/3/14
to androi...@googlegroups.com
Aerik,
seaching for another argument, i 'be found this interesting one, but i've a question: why do you load the image via an XHR request and not using the fileReader API? even if not supported by androidscript surely you can have a webView, HTML5 compliant imho, that read the file in a var that is then read in js.
correct me if i'm wrong
thanks and regards
Salvatore

salvatore fusto

unread,
Oct 22, 2015, 3:15:45 AM10/22/15
to AndroidScript
Hi all,
i'm developing an app that among other take a picture, zip it and send it to a server. i tested this function but i always get a 0 status in reading the file, surely for cross domain problems; then i tried to load the image using the filereader api with the same problem: my app must run in standard devide so no rooted.
can anyone help me?

PedroD

unread,
Oct 22, 2015, 5:46:50 PM10/22/15
to AndroidScript
jpg are already compressed, I think you will find that if you save the jpg and then compress it from the file browser you will find that the zip file is bigger than the original.

A 0 status - what I think you mean is the file has zero size.

Pete

Chris

unread,
Oct 22, 2015, 7:47:58 PM10/22/15
to AndroidScript
Actually, the reason you are getting a 0 is because app.ReadFile only reads text and can not open a jpg, jpeg, or any image.

salvatore fusto

unread,
Oct 23, 2015, 3:06:30 AM10/23/15
to AndroidScript
Pedro,
i agree with you about compression, but generally i use zip format to send files; about the status=0, the image is not zero size, as it is displayed in an image control.
my snippet code:
function uploadImage(){
    var oReq = new XMLHttpRequest();
    oReq.open("GET",myImg, true);
    oReq.responseType = "arraybuffer"
    oReq.send();
    oReq.onload=function(e){
       
        alert(oReq.readyState+" - "+oReq.status+" - "+ JSON.stringify(oReq.response));
        
        if (oReq.readyState==4)
        {
            var blob = new Blob([oReq.response], {type: "image/jpeg"});

             
        }
        
    }    
    //oReq.responseType = "arraybuffer";
    //oReq.send();
    //oReq.responseType="blob";
    //alert(JSON.stringify(oReq))
   
}

salvatore fusto

unread,
Oct 23, 2015, 3:11:06 AM10/23/15
to AndroidScript
Pedro,
i agree with you about compression, but generally i use zip format to send files; about the status=0, the image is not zero size, as it is displayed in an image control.
my snippet code:
function uploadImage(){
    var oReq = new XMLHttpRequest();
    oReq.open("GET",myImg, true);
    oReq.responseType = "arraybuffer"
    oReq.send();
    oReq.onload=function(e){
       
        alert(oReq.readyState+" - "+oReq.status+" - "+ JSON.stringify(oReq.response));
        
        if (oReq.readyState==4)
        {
            var blob = new Blob([oReq.response], {type: "image/jpeg"});
.....             
        }
        
    }    
   
}
the alert display: 4 - 0 - {"byteLength":18102} and the next blob creation fails: why only the image length without content, is read?

Chris, as you see, i don't use app.ReadFile to read my image.
thanks and regards

Rocco Cogliano

unread,
Jun 24, 2016, 9:50:55 AM6/24/16
to DroidScript
Hi, and if I want upload a generic file on my server  (txt file for example),  what changes? 
And on server which php code I will have?

R
Reply all
Reply to author
Forward
0 new messages