Pet*_*hko 5 javascript encoding utf-8 character-encoding windows-1252
我尝试使用 Javascript 创建 CSV 文件下载。
\n\n我们需要将数据从我们的网站导出到第三方程序,创建和下载效果很好。只有一个问题,我需要以 ANSI (Windows-1252) 编码的 CSV 文件 - 第 3 方程序非常旧,无法理解多字节编码。我的文件以 UTF-8 格式提供,到目前为止我还没有找到将文件转换为 ANSI 的方法,我总是得到 UTF-8-Content...
\n\n当前的修补程序是打开文件并手动将编码更改为 ANSI,但这并不好,而且对于我公司中的一些人来说很难做到(有时他们会忘记它,因为他们对 PC 不太熟悉)。
\n\n我想要一个可以在 ANSI 格式(而不是 UTF-8 格式)中使用的文件。我可以用 PHP 转换文件,它具有正确的编码和内容,但我在服务器上没有写访问权限,这就是为什么我需要使用 AJAX 动态下载文件。
\n\n我在 Stackoverflow 上使用了像他这样的解决方案:/sf/answers/1546258381/
\n\n但我得到 UTF-8 作为内容。
\n\n我退后一步,尝试在一个非常简单的页面上使用 JavaScript 进行转换,但我也得到了 UTF-8...:
\n\n<html>\n <head>\n <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />\n <title>My File-Download</title>\n <script type=\'text/javascript\' src=\'encoding.js\'></script>\n <script type=\'text/javascript\' src=\'encoding-indexes.js\'></script>\n <script type=\'text/javascript\' src=\'FileSaver.min.js\'></script>\n <script type=\'text/javascript\'>\n <!--\n\n /**\n * Downloads the Text as File\n *\n * @param {string} filename - Name of the File\n * @param {string} text - Content of the File\n */\n function downloadExportFile(filename, text) {\n var encodedText = new TextEncoder("windows-1252", {NONSTANDARD_allowLegacyEncoding: true}).encode([text]);\n var blob = new Blob([encodedText], {type: \'text/plain;charset=windows-1252;\'});\n saveAs(blob, filename);\n }\n\n // -->\n </script>\n </head>\n <body>\n <a href="#" onclick="downloadExportFile(\'export.csv\', \'Dragicevic,Peter,,1,Stra\xc3\x9fe 4,21027,Hamburg,43,,,,,,,,\');">Download Windows-1252 CSV-File</a>\n </body>\n</html>\nRun Code Online (Sandbox Code Playgroud)\n\n我没有找到太多关于将 UTF-8 转换为 ANSI 的信息(但在另一个方向上有大量的解决方案)。有人知道使用 JavaScript 获取 ANSI (Windows-1252) 文件的解决方案吗?
\n我希望它有帮助!
\n\n\n\n下载encoding-indexes.js 和encoding.js 文件
\n\nHTML 示例:
\n\n<!DOCTYPE html>\n<html>\n<head>\n <meta charset="utf-8" />\n\n <title></title>\n\n <script>\n window.TextEncoder = window.TextDecoder = null;\n </script>\n\n <script type="text/javascript" src="encoding-indexes.js"></script> \n <script type="text/javascript" src="encoding.js"></script>\n\n <script>\n function BlobDownload(Filename, Bytes, Mimetype) {\n var filData = new Blob(Bytes, { type: Mimetype });\n if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE\n window.navigator.msSaveOrOpenBlob(filData, Filename);\n } else { // for Non-IE (chrome, firefox etc.)\n var a = document.createElement("a");\n document.body.appendChild(a);\n a.style = "display: none";\n var filUrl = URL.createObjectURL(filData);\n a.href = filUrl;\n a.download = Filename;\n a.click();\n a.remove();\n }\n };\n\n function download() {\n var bytes = new TextEncoder("windows-1252", { NONSTANDARD_allowLegacyEncoding: true }).encode("Eu n\xc3\xa3o tenho mais d\xc3\xbavidas");\n\n BlobDownload("test_windows1252_encoding.txt", [bytes]);\n BlobDownload("test_windows1252_encoding.csv", [bytes], "text/csv");\n }\n </script>\n </head>\n <body>\n <button onclick="download()">Download</button>\n </body>\n</html>\nRun Code Online (Sandbox Code Playgroud)\n