开发者

Generate blob data from json object in node/express

How can I create blob data from json object ?

I tried few approaches as shown below but front-end always receives empty blob.


Approach: 1开发者_C百科

const buffer = require("buffer"); 

const myJsonFileData = { "id": 1, "name":"abc"};

const str = JSON.stringify(myJsonFileData );

const bytes = new TextEncoder().encode(str);

const blobForJsonFile = new buffer.Blob([bytes], {
    type: "application/json;charset=utf-8"
});


return res.send({filename:'xxx.json', blob: blobForJsonFile });

But front-end always receives

blob: {}
filename: "xxx.json"

If I get proper blob data, at FE, I want create xxx.json file.


Approach: 2

const Blob = require("buffer").Blob;

const myJsonFileData = { "id": 1, "name":"abc"};

const blob1 = new Blob([JSON.stringify(myJsonFileData , null, 2)], {
                        type: 'application/json',
              });

return res.send({filename:'xxx.json', blob: blobForJsonFile });

But it does give same empty result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜