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

Revision history [back]

click to hide/show revision 1
initial version

posted 2019-10-01 20:37:41 +0200

tmy gravatar image

How to send a file using XMLHttpRequest

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

How to send a file using XMLHttpRequest

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.

How to send a file using XMLHttpRequest

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.