下面是使用文件輸入控制讀取一個文件中的示例代碼和一些文件的屬性沿顯示第一行:
<input type="file" id="fileinput" />
<script type="text/javascript">
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var f = evt.target.files[0];
if (f) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
alert("Got the file.n"
+"name: " + f.name + "n"
+"type: " + f.type + "n"
+"size: " + f.size + " bytesn"
+ "starts with: " + contents.substr(1, contents.indexOf("n"))
);
}
r.readAsText(f);
} else {
alert("Failed to load file");
}
}
document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>
您可以使用閱讀文件使用HTML5的FileReader API。嘗試加載錯誤的編碼時內容被破壞後嘗試轉換文件是沒有意義的。
,如果你想要去的內心深處試着看下面的文章:
htmlgoodies
html5rocks
如果你可以使用節點,看在覈心模塊, 'string_decoder'。 – YvesLeBorg
ANSI使用什麼代碼頁,完全是?沒有像「ANSI」那樣的東西。 – RocketNuts