We have moved to a new Sailfish OS Forum. Please start new discussions there.
0

How to send a file using XMLHttpRequest

asked 2019-10-01 20:37:41 +0300

tmy gravatar image

updated 2019-10-01 22:27:40 +0300

I'm developing a sailfish app in QML + javascript. Is there a way to send a file (image) to an endpoint using HTTP POST? Apparently QML javascript does not support FormData, which would have been a solution.

Currently I'm using something like this:

function api_post(callback, end_point, send_data, params) {
    var xhr = new XMLHttpRequest();
    params = params || {};

    var parameters = "";
    for (var p in params) {
        parameters += "&" + p + "=" + params[p];
    }
    var request = api_url + end_point + "?personToken=" + person_token + "&access_token=" + access_token + parameters;
    console.log(request)
    send_data = JSON.stringify(send_data)
    console.log(send_data)

    xhr.onreadystatechange = function() {processRequest(xhr, callback);};

    xhr.open('POST', request, true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.setRequestHeader("Accept", "application/json");
    xhr.send(send_data);
}

function processRequest(xhr, callback, e) {

    if (xhr.readyState === 4) {
        console.log(xhr.status)
        var response
        try {
            response = JSON.parse(xhr.responseText);
        }
        catch (e) {
            response = xhr.responseText
        }
        callback(xhr.status, response);
    }
}

But this only works for text data.

edit retag flag offensive close delete

Comments

This works for me. The code is used to control an IR LED strip via WiFi using a NodeMCU;

Rectangle { id: brightnessUp; width: 80; height: 80; radius: 40; color: "white"
    MouseArea {
        anchors.fill: parent
        onClicked: {
            getData()
            function getData(url) {
                var xhttp = new XMLHttpRequest()
                xhttp.onreadystatechange = function(myxhttp) {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                        console.log("Brightness UP")
                    }
                    return function() {
                        if(myxhttp.readyState === 4) callback(myxhttp)
                    }
                }
                xhttp.open("GET", "http://192.168.0.105/ir?code=16726725")
                xhttp.send('')
            }
        }
    }
}

If there is a less clunky way of doing this, I'd like to see it :)

RELATED: https://together.jolla.com/question/16995/xmlhttprequest-responsetext-cutting-off-data/

Spam Hunter ( 2019-10-01 21:22:12 +0300 )edit
1

My question is about how to send an image file from a sailfish app to a remote endpoint. I have already figured out how to handle text data (yeah, the same clunky way :) ), but not how to send a binary file.

tmy ( 2019-10-01 22:15:04 +0300 )edit

My bad, speed reading again!

Spam Hunter ( 2019-10-02 12:34:56 +0300 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-10-02 09:52:32 +0300

Max-Might gravatar image

updated 2019-10-02 10:10:57 +0300

I have not tested it but you could try this: sending typed arrays as binary data.

You just have to read your image (or any other file) as an int array and use the same xhr.send() call.

Alternatively you can check out if duperagent supports this.

edit flag offensive delete publish link more

Comments

Thanks! I'll try this next and report back.

tmy ( 2019-10-02 17:02:30 +0300 )edit

Apparently typed arrays are not supported in QML either.

tmy ( 2019-10-06 17:30:58 +0300 )edit
Login/Signup to Answer

Question tools

Follow
2 followers

Stats

Asked: 2019-10-01 20:37:41 +0300

Seen: 999 times

Last updated: Oct 02 '19