Javascript - >下载以ISO-8859-1 / Latin1 / Windows-1252编码的CSV文件

时间:2015-07-27 13:23:20

标签: javascript macos encoding utf-8 iso-8859-1

我已经将一个小工具整合在一起,从Amazon CSV订单数据中提取出货数据。它到目前为止工作。这是JS Bin的简单版本:http://output.jsbin.com/jarako

对于打印邮票/运输标签,我需要一个文件上传到德国邮政和其他包裹服务。我使用了一个在stackoverflow上找到的小函数saveTextAsFile。到目前为止一切都很好。输出文本区域或下载文件中没有错误显示的特殊字符(äöüß...)。

所有这些德国邮政/包裹服务网站仅接受latin1 / iso-8859-1编码文件进行上传。但我下载的文件总是utf-8。如果我上传它,所有特殊字符(äöüß......)都会出错。

我该如何改变?我仍然搜索了很多。我试过了,但是:

将工具的charset设置为iso-8859-1:

<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

但结果是:现在我仍然在输出文本区域和下载文件中有错误的特殊字符。如果我将其上传到帖子网站,我仍会得到更多错误的字符。此外,如果我在CODA编辑器中检查编码,它仍然说下载的文件是UTF-8。

saveTextAsFile函数使用var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});。可能有一种方法可以设置下载的字符集!?

function saveTextAsFile()
{
    var textToWrite = $('#dataOutput').val();
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    var fileNameToSaveAs = "Brief.txt";

    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null)
    {
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    }
    else
    {
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }

    downloadLink.click();
}

无论如何,必须有一种方法来下载其他编码的文件,因为该网站使用自己。我从中下载CSV文件的亚马逊网站是UTF-8编码的。但是从那里下载的CSV文件是Latin1(iso-8859-1),如果我在CODA中检查...

3 个答案:

答案 0 :(得分:4)

滚动到真正解决方案的更新!

因为我没有回答,所以我搜索的越来越多。看起来Javascript中没有解决方案。我在javascript中生成的每个测试下载都是UTF-8编码的。看起来Javascript仅用于UNICODE / UTF-8,或者只有在使用以前的HTTP传输再次传输数据时才会(可能)应用其他编码。但对于在客户端上运行的Javascript,不会发生额外的HTTP传输,因为数据仍在客户端上。

我现在帮助我在服务器上构建一个小PHP脚本,我通过GET或POST请求发送数据。它将编码转换为latin1 / ISO-8859-1并将其下载为文件。这是一个ISO-8859-1文件,带有正确编码的特殊字符,我可以将其上传到上述邮政和包裹服务网站,一切看起来都不错。

latin-download.php :(非常重要的是保存PHP文件本身也在ISO-8859-1中,以使其工作!!)

<?php
$decoded_a = urldecode($_REQUEST["a"]);
$converted_to_latin = mb_convert_encoding($decoded_a,'ISO-8859-1', 'UTF-8');
$filename = $_REQUEST["filename"];
header('Content-Disposition: attachment; filename="'.$filename.'"; content-type: text/plain; charset=iso-8859-1;');
echo $converted_to_latin;
?>

在我使用的javascript代码中:

<a id="downloadlink">Download File</a>

<script>
var mydata = "this is testdata containing äöüß";

document.getElementById("downloadlink").addEventListener("click", function() {
    var mydataToSend = encodeURIComponent(mydata);
    window.open("latin-download.php?a=" + mydataToSend + "&filename=letter-max.csv");
}, false);
</script>

对于更大量的数据,您必须从GET切换到POST ...

2016年2月8日更新

半年后的今天,我在PURE JAVASCRIPT找到了解决方案。使用inexorabletash/text-encoding。这是Encoding Living Standard的填充物。该标准包括解码旧编码,如latin1(&#34; windows-1252&#34;),但它禁止编码为这些旧的编码类型。因此,如果您使用浏览器实现的window.TextEncoder函数,它只提供UTF编码。但是,polyfill solution提供了遗留模式,它也允许编码为旧的编码,如latin1。

我是这样用的:

<!DOCTYPE html>
<script>
// 'Copy' browser build in TextEncoder function to TextEncoderOrg (because it can NOT encode windows-1252, but so you can still use it as TextEncoderOrg()  )
var TextEncoderOrg = window.TextEncoder;   
// ... and deactivate it, to make sure only the polyfill encoder script that follows will be used 
window.TextEncoder = null;  

</script>
<script src="lib/encoding-indexes.js"></script>  // needed to support encode to old encoding types
<script src="lib/encoding.js"></script>  // encording polyfill

<script>

function download (content, filename, contentType) {
    if(!contentType) contentType = 'application/octet-stream';
        var a = document.createElement('a');
        var blob = new Blob([content], {'type':contentType});
        a.href = window.URL.createObjectURL(blob);
        a.download = filename;
        a.click();
}

var text = "Es wird ein schöner Tag!";

// Do the encoding
var encoded = new TextEncoder("windows-1252",{ NONSTANDARD_allowLegacyEncoding: true }).encode(text);

// Download 2 files to see the difference
download(encoded,"windows-1252-encoded-text.txt");
download(text,"utf-8-original-text.txt");

</script>

encoding-indexes.js文件大约500kb,因为它包含所有编码表。因为我只需要windows-1252编码,为了我的使用我删除了这个文件中的其他编码。所以现在只剩下632字节。

答案 1 :(得分:2)

您不能强制Web服务器以给定的编码发送数据,只能礼貌地询问它。您只需转换为所需格式的方法就是正确的方法。

如果您想避免使用PHP脚本,您可能很幸运在创建Blob时将编码指定为参数:

var textFileAsBlob = new Blob(textToWrite, {
  type: 'text/plain;charset=ISO-8859-1', 
  encoding: "ISO-8859-1"
});

有关详细信息,请参阅Specifying blob encoding in Google Chrome

答案 2 :(得分:0)

问题不在于编码,而在于某些应用程序中特殊字符显示错误的事实,例如微软Excel。 UTF-8 适合显示所有特殊的德语字符。您可以通过在 csv 前添加字节顺序标记 (BOM) 来解决此问题。

False

基于this github post的解决方案