0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ajaxでバイナリデータを受け取ってimageタグにロードする

Posted at

以下の記事を参考にさせていただきました
https://qiita.com/Yarimizu14/items/f56123c738f12ad1844a

仕様は入力フォームにデータを入力して、サーバサイドにJSON文字列を送付、サーバサイドでJSON文字列をパースしてImageを生成して、レスポンスを返す、JavaScriptでレスポンスを受け取って画面内のImageタグにロードします。エラーハンドリンクは省略しています。

JavaScript

var oReq = new XMLHttpRequest();
oReq.open("POST", "http://localhost:8080/target", true);
oReq.responseType = "arraybuffer";
oReq.setRequestHeader("Content-Type", "application/json");
oReq.onload = function (oEvent) {
    var arrayBuffer = oReq.response;
    if (arrayBuffer) {
        var byteArray = new Uint8Array(arrayBuffer);
        var binaryData = "";
        for (var i = 0; i < byteArray.byteLength; i++) {
            binaryData += String.fromCharCode(byteArray[i]);
        }
        var img = document.getElementById("targetid");
        img.src = "data:image/png;base64," + window.btoa(binaryData);
    }
};
oReq.send(JSON.stringify(json));

こちらを適宜修正して、資産運用の推移を画面から入力して、グラフを返すような機能を作ります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?