使用 UTF-16LE 编码的 Blob URL [英] Blob URL with UTF-16LE encoding

查看:56
本文介绍了使用 UTF-16LE 编码的 Blob URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串 %22%00%41%00%22%00%09%00%22%00%42%00%22%00 这是 UTF-16LE 等价于"A"\t"B".(\t 是制表符.)

I have this string %22%00%41%00%22%00%09%00%22%00%42%00%22%00 which is the UTF-16LE equivalent of "A"\t"B". (The \t is a Tab char.)

我正在尝试构建一个 Blob,然后为其构建一个 URL,但输出未解码为正确的实体.

I am trying to construct a Blob and then a URL for it, but the output isn't decoded to proper entities.

var blob=new Blob([stringHere],{type:'text/csv;charset=UTF-16LE;'});
var blobUrl=URL.createObjectURL(blob);

无论如何要告诉 Blob 字符串编码,以便它在 Excel 中打开时看起来正确(在这种特殊情况下)?

Is there anyway to tell the Blob the string encoding so that it looks properly when opened in Excel (in this particular case)?

我需要 UTF-16LE,否则使用 UTF-8 会导致 Excel 无法正确解析 .csv 文件.

I need UTF-16LE, otherwise using UTF-8 will result in Excel no parsing the .csv file properly.

谢谢.

推荐答案

看起来文件中需要一个 utf-16 le bom,而 js 中的字符串无法做到这一点,因此您必须使用字节数组.看下面的例子

It looks like you need a utf-16 le bom in the file, which you can't do with a string in js, so you'll have to use a byte array. See example below

var stringHere = '%ff%fe%22%00%41%00%22%00%09%00%22%00%42%00%22%00';
var byteArray = [];
stringHere.replace(/([0-9a-f]{2})/gi, function(d){
    byteArray.push(parseInt(d, 16));
});
var blob=new Blob([new Uint8Array(byteArray)],{type:'text/csv;charset=UTF-16LE;'});
var blobUrl=URL.createObjectURL(blob);

这篇关于使用 UTF-16LE 编码的 Blob URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆