down123321123 2013-09-17 01:31
浏览 69

使用JavaScript读取/写入文件时的字符编码

I'm currently having some issues with character encoding in client side JavaScript. My basic program flow is this: client side JavaScript reads a local text file using the nifty FileReader. I then do a bunch of edits on the string, and then I try to offer a way for the user to download the new, altered file. Here's my issue, the file I'm reading from is (according to Notepad++) encoded in ANSI, and the file I want to write also needs to be encoded in ANSI.

When I try reading from the file like this:

reader.readAsText(this.files[0], "ANSI");
...
cachedFile = e.target.result.split("
");
console.log(cachedFile[179544]);

My result is something along the lines of this (the Î character isn't read properly):

name="�le-de-France" 

However, when I use ISO-8859-1 as the encoding parameter (a completely random choice), for some reason the result is correct:

name="Île-de-France" 

So there's a large chance I have no idea what's happening. So I left it with ISO-8859-1 encoding, did my various edits, and then tried to prepare it for downloading. I can't simply POST this to my server uncompressed and prepare a file for download because this file is, frankly, rather large (~14 mb). It does however compress very nicely since it's plain text. The issue is that any JavaScript compression library I've found (Like jszip which nicely lets you generate a file and stick it in a .zip) seems to maintain JavaScript's internal string encoding, which I believe is UTF-16. The .zip file is also encoded as base64 (which I just decoded on my PHP server). Doing this, of course, gives a final result of something like this:

name="ÃŽle-de-France"

So here's my issue, I have a file encoded in ANSI, I parse it using ISO-8859-1, I edit it in UTF-16, and I need to find a way to get it back into ANSI and onto a person's desktop. Is there a standard way to either convert the JavaScript string to ANSI before compressing it so I can just offer the compressed file to my user to download? Or is there a way to uncompress the string on the server side using PHP, convert to ANSI, and then offer it for download? Just for reference, my current PHP code is simply this:

<?php 

 $res = $_POST["saveString"];
 $maybe = base64_decode($res);
 header('Content-Type: application/download');
 header('Content-Disposition: attachment; filename="genSave.zip"');
 header("Content-Length: " . strlen($maybe));
 echo $maybe;

?>
  • 写回答

2条回答 默认 最新

  • dongxieli3839 2013-09-17 01:42
    关注

    My guess is [I will delete the answer if incorrect] that your encoding value is not valid. What Notepad calls "ANSI" is Windows-1252, nearly the same as ISO-8859-1. What do you expect "ANSI" to be other than ISO-8859-1? This should work:

    reader.readAsText(this.files[0], "iso-8859-1");
    reader.readAsText(this.files[0], "windows-1252");
    

    See the W3C spec for reference.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大