-1

I am using Bing Maps where you can use a POST call to get image data (png/jpeg/gif).

https://learn.microsoft.com/en-us/bingmaps/rest-services/imagery/get-a-static-map

Neither can I render the image to the user nor is it possible to download the file and display it when opened locally (the download works but the image file won't show an image).

This is the code that handles the image data from the POST request to the bing maps api:

// rsp contains UTF-8 image data (png)

let reader = new FileReader();
let file = new File([rsp], 'test.png');

// trying to render to user
reader.onloadend = function () {
    document.getElementById('img').src = 'data:image/png;base64,' + reader.result.substr(37); // substr(37) will get base 64 string in a quick and dirty way
};

reader.readAsDataURL(file);

// trying to make the image downloadable (just for testing purposes)

var a = document.createElement("a"),
    url = URL.createObjectURL(file);
a.href = url;
a.text = "Test";
a.download = 'test.png';
document.body.appendChild(a);
Julius S.
  • 664
  • 9
  • 21

1 Answers1

0

The solution was to use a native XMLHttpRequest with responseType 'blob' or 'arraybuffer' to handle the binary server response (https://stackoverflow.com/a/33903375/6751513).

    var request = new XMLHttpRequest();
    request.open("POST", bingMapsPOSTEndpoint + '&' + queryParamsString, true);
    request.responseType = "blob";

    request.onload = function (e) {

        var dataURL = URL.createObjectURL(request.response);
        document.getElementById('img').src = dataURL;

    };

    request.send();
Julius S.
  • 664
  • 9
  • 21