- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我开发了一个小工具来从 Amazon CSV 订单数据中提取运输数据。到目前为止它有效。这是 JS Bin 的简单版本:http://output.jsbin.com/jarako
为了打印邮票/运输标签,我需要一个文件来上传到德国邮政和其他包裹服务。我使用了一个在 stackoverflow 上找到的小函数 saveTextAsFile
。到目前为止一切都很好。在输出文本区域或下载的文件中没有错误显示特殊字符 (äöüß...)。
所有这些德国邮政/包裹服务网站仅接受 latin1/iso-8859-1 编码文件上传。但是我下载的文件总是utf-8。如果我上传它,所有特殊字符 (äöüß...) 都会出错。
我怎样才能改变这个?我仍然搜索了很多。我已经尝试过,即:
将工具的字符集设置为 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 编码。但是如果我在 CODA 中检查它,从那里下载的 CSV 文件是 Latin1 (iso-8859-1)...
最佳答案
向下滚动到更新以获得真正的解决方案!
因为没有得到答案,所以我找了越来越多。看起来 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 的 polyfill .该标准包括对 latin1(“windows-1252”)等旧编码的解码,但它禁止编码为这些旧编码类型。因此,如果您使用浏览器实现的 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 字节了。
关于Javascript -> 下载以 ISO-8859-1/Latin1/Windows-1252 编码的 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654292/
Google Web字体上的某些字体支持多个“字符集”。关键是,如果我使用的Web字体仅提供“拉丁”字形,那么将页面翻译成不支持该字形的语言的用户将清楚地注意到困惑的文本。 我希望我的网络字体能够支持
我有(我相信)一个独特的情况;有点困惑。这是交易。 情况:我有一个单一语言的网站,其中所有内容(内容、链接)都使用非拉丁字符。 (西里尔文)(utf-8) 该网站旨在将全局读/说俄语的人联系起来。 问
我在数据库中有 10 个表。其中 9 个只存储 Latin-1 支持的标准 ascii 1 字节字符的数据。其中 1 个要求我存储仅受 UTF8 支持的特殊字符。我想使用相同的 MySQL 连接对象(
我想在 Windows 中从 eclipse 执行 Pig 脚本。我已关注this link 。但它不起作用。是否需要任何插件才能做到这一点?或者只有 pig.jar 就可以了? 最佳答案 试试这个,
题目地址:https://leetcode.com/problems/goat-latin/description/ 题目描述 Asentence S is given, composed of
我正在开发一个将普通单词转换为 pig 拉丁语的函数,但我无法将所有内容组合在一起;它必须适用于加州、手套和八。什么地方不正常? function translate(word) {
我正在尝试编写一个 pig 拉丁语翻译器,但我的网页一直显示未定义,并且无法从文本区域读取。 html 看起来不错,但最终用户需要输入的文本区域中的文本未正确显示。我尝试使用 .textContent
我写这封信是因为今天我遇到了一个问题,尽管到处搜索并尝试了许多不同的语句,但我无法以任何方式解决。 我有这个输入文件: 3 {(car pen house glass)} 5 {(battery ph
我有一个以前用 Latin-1 编码的文件。现在,当我打开这个文件时,我只得到原始编码。即状态行中的 -t:。文件中可能有一些非 Latin-1 字符,至少可以打开其他 Latin-1 文件。 我只想
我正在编写一个程序,它接受一个字符串,将其拆分为单词,将单词转换为 pig 拉丁语,然后返回结果字符串。我已经让它工作到一定程度了。 例如,如果我在程序中输入这些不以元音开头的单词,我会得到: pig
#include #include #include #define isvowel(v) (v=='a' || v=='e' || v=='i' || v=='o' || v=='u') in
所以我是编码的新手,我遇到了一些问题...我的程序应该要求用户输入,并且需要假设所有输入都是小写...并且需要假设没有额外的空格,并且需要假设它以句点结尾。然后该程序会将文本翻译成 pig latin
希望您一切顺利。 我对 Java 和这个网站都很陌生。虽然这可能看起来很长,但我只需要两件事的帮助,所以请帮忙,就像我说的,我对这一切都很陌生,所以越彻底越好。我必须做一个项目,我们必须将常规英语单词
我刚刚收到一个 SQL 插入脚本,但它在重复键输入时失败了: 我正在尝试插入: 1)蒙大拿 2)蒙大拿 我的表都是utf8_spanish2_ci, 谁能解释为什么会这样? 最佳答案 utf8_spa
我想编写一个函数,它将接受一个字符串并将单词转换为 Pyg 拉丁语。这意味着: 如果单词以元音开头,则在末尾添加“-way”。示例:“ant”变成“ant-way”。 如果单词以辅音簇开头,则将该辅音
所以我应该使用 stringConvertToPigLatin(string word) 函数将英语单词转换为 Pig Latin。我在网上能找到的所有答案都是使用 char[],我不允许这样做。如果
这看起来像是家庭作业,但请放心,这不是家庭作业。只是我们在 c++ 类(class)中使用的书中的一个练习,我正在尝试提前阅读指针。 书上的练习告诉我将一个句子拆分成标记,然后将它们中的每一个转换成
所以,我尝试并尝试制作这个 c++ pig 拉丁语程序,但它就是行不通。这是我的代码: int main() { string tmp = ""; char a; cout << "String: "
我最近在工作中遇到了这个问题,是关于pig flatten的。我用一个简单的例子来表达它 两个文件 ===文件1=== 1_a 2_b 4_d ===file2(制表符分隔)=== 1个 2乙 3c
在 Pig 中执行多级过滤后,我得到以下结果 - (2343433,Argentina,2015,Sci-Fi) (2343433,France,2015,Sci-Fi) (2343433,Germa
我是一名优秀的程序员,十分优秀!