简体   繁体   English

如何将 javascript 对象转换为 utf-8 Blob 以供下载?

[英]How to Convert a javascript object to utf-8 Blob for download?

I've been trying to find a solution that works but couldn't find one.我一直在努力寻找一种可行但找不到的解决方案。

I have an object in javascript and it has some non-english characters in it.我在 javascript 中有一个对象,其中有一些非英文字符。
I'm trying the following code to convert the object to a blob for download.我正在尝试使用以下代码将对象转换为 blob 以供下载。
When I click to download the content, when opening the downloaded JSON the non-English characters are gibberish.当我点击下载内容时,打开下载的 JSON 时非英文字符是乱码。

It's a simple object like this one: {name: "שלומית", last: "רעננה"}这是一个像这样的简单对象: {name: "שלומית", last: "רעננה"}

function setJSONForDownload(obj) {
    obj = obj || []; // obj is the array of objects with non-english characters
    const length = obj.length;
    if (length) {
      const str = JSON.stringify(obj);
      const data = encode( str );

      const blob = new Blob( [ data ], {
        type: "application/json;charset=utf-8"
     });

      const url = URL.createObjectURL( blob );
      const downloadElem = document.getElementById('download');
      downloadElem.innerText = `Download ${length} pages scraped`;
      downloadElem.setAttribute( 'href', url );
      downloadElem.setAttribute( 'download', 'data.json' );
    }
    else {
      document.getElementById('download').innerText = `No data to download...`;
    }
}

function encode (s) {
  const out = [];
  for ( let i = 0; i < s.length; i++ ) {
    out[i] = s.charCodeAt(i);
  }
  return new Uint8Array(out);
}

Your encode function is broken, as it casts charcodes to bytes.您的encode功能已损坏,因为它将字符代码转换为字节。 Don't try to implement this yourself, just use the Encoding API :不要尝试自己实现,只需使用Encoding API

const str = JSON.stringify(obj);
const bytes = new TextEncoder().encode(str);
const blob = new Blob([bytes], {
    type: "application/json;charset=utf-8"
});

Calling new Blob([DOMString]) will automatically convert your DOMString (UTF-16) to UTF-8.调用new Blob([DOMString])会自动将您的DOMString (UTF-16) 转换为 UTF-8。

So all you need is new Blob( [JSON.stringify(obj)] ) .所以你只需要new Blob( [JSON.stringify(obj)] )

 setJSONForDownload([{ name: "שלומית", last: "רעננה"}]); function setJSONForDownload(obj) { obj = obj || []; const length = obj.length; if (length) { // DOMString const str = JSON.stringify(obj); // text/plain;UTF-8 const blob = new Blob([str]); const url = URL.createObjectURL(blob); const downloadElem = document.getElementById('download'); downloadElem.innerText = `Download ${length} pages scraped`; downloadElem.setAttribute('href', url); downloadElem.setAttribute('download', 'data.json'); } else { document.getElementById('download').innerText = `No data to download...`; } }
 <a id="download">dl</a>

I found a nice block of code that solved my issue.我找到了一段很好的代码来解决我的问题。
Thanks to 'pascaldekloe' ( https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330 ).感谢“pascaldekloe”( https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330 )。

Just changed the encode method to the following:只是将编码方法更改为以下内容:

function encode(s) {
    var i = 0, bytes = new Uint8Array(s.length * 4);
    for (var ci = 0; ci != s.length; ci++) {
        var c = s.charCodeAt(ci);
        if (c < 128) {
            bytes[i++] = c;
            continue;
        }
        if (c < 2048) {
            bytes[i++] = c >> 6 | 192;
        } else {
            if (c > 0xd7ff && c < 0xdc00) {
                if (++ci >= s.length)
                    throw new Error('UTF-8 encode: incomplete surrogate pair');
                var c2 = s.charCodeAt(ci);
                if (c2 < 0xdc00 || c2 > 0xdfff)
                    throw new Error('UTF-8 encode: second surrogate character 0x' + c2.toString(16) + ' at index ' + ci + ' out of range');
                c = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
                bytes[i++] = c >> 18 | 240;
                bytes[i++] = c >> 12 & 63 | 128;
            } else bytes[i++] = c >> 12 | 224;
            bytes[i++] = c >> 6 & 63 | 128;
        }
        bytes[i++] = c & 63 | 128;
    }
    return bytes.subarray(0, i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM