问题描述
我必须通过csv文件导出表格.
I have to export table by csv file.
csv文件数据是按Blob类型来自服务器的.
csv file data is from server by Blob type.
Blob {size: 2067, type: "text/csv"}
async exportDocumentsByCsv() {
this.commonStore.setLoading(true)
try {
const result = await DocumentSearchActions.exportDocumentsByCsv({
searchOption: this.documentSearchStore.searchOption
})
// first
// const blob = new Blob([result.body], { type: 'text/csv;charset=utf-8;' })
// second
// const blob = new Blob([`\ufeff${result.body}`], { type: 'text/csv;charset=utf-8;' })
const blob = result.body
console.log('result.body', result.body)
const fileName = `document - search - result.csv`
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
// for IE
window.navigator.msSaveOrOpenBlob(blob, fileName)
} else {
FileSaver.saveAs(blob, fileName)
}
this.commonStore.setLoading(false)
} catch (err) {
alert(err.errorMessage)
this.commonStore.setLoading(false)
}
}
由于我的语言,我必须设置utf-8.
I have to set utf-8 or else because of my language.
我试图解决此问题,但我不知道如何解决.
I tried to fix this issue, but I don't know how to fix it.
我通过使用 \ ufeff
搜索来解决此问题,但是当我尝试像这样使用时第二种方式,对我不起作用.
I searched fix this issue by using \ufeff
but When I try to use this likesecond way, It doesn't work for me.
| [object | Blob] |
推荐答案
Blob 不会为您处理编码,它所看到的只是二进制数据.唯一的转换是在构造函数的BlobsList
Blob doesn't take care of the encoding for you, all it sees is binary data. The only conversion it does is if you pass in an UTF-16 DOMString in the constructor's BlobsList
您所处的最佳状态是将应用程序中的所有内容(从服务器到前端)都设置为UTF-8,并确保使用UTF-8发送所有内容.这样,您将能够直接保存服务器的响应,并将其保存在UTF-8中.
The best in your situation is to set up everything in your application from server to front as UTF-8 and to ensure that everything is sent using UTF-8. This way, you will be able to directly save the server's response, and it will be in UTF-8.
现在,如果要将文本文件从已知编码转换为UTF-8,则可以使用 TextDecoder ,它可以将二进制数据的ArrayBuffer视图从给定编码解码为DOMString,然后可以将其用于生成UTF-8 Blob:
Now, if you want to convert a text file from a known encoding to UTF-8, you can use a TextDecoder, which can decode a ArrayBuffer view of the binary data from a given encoding to DOMString, which can then be used to generate an UTF-8 Blob:
/* const data = await fetch(url)
.then(resp=>resp.arrayBuffer())
.then(buf => new Uint8Array(buf));
*/
const data = new Uint8Array([147, 111, 152, 94 ]);
// the original data, with Shift_JIS encoding
const shift_JISBlob = new Blob([data]);
saveAs(shift_JISBlob, "shift_JIS.txt");
// now reencode as UTF-8
const encoding = 'shift_JIS';
const domString = new TextDecoder(encoding).decode(data);
console.log(domString); // here it's in UTF-16
// UTF-16 DOMStrings are converted to UTF-8 in Blob constructor
const utf8Blob = new Blob([domString]);
saveAs(utf8Blob, 'utf8.txt');
function saveAs(blob, name) {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = name;
a.textContent = 'download ' + name;
document.body.append(a);
}
a{display: block;}
这篇关于如何在Blob类型中使用UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!