我已經瀏覽了幾個小時,查看了網絡上的不同選項,試圖理解 - 而不僅僅是複製 - 如何加載文本文件。我找不到任何我發現的例子,也就是說,直到我從鉻改爲Firefox。作爲一個例子,在計算器問題的代碼:HTML5 File api, reading in an xml/text file and displaying it on the page?我寫在這裏爲簡便起見:在html5 + javascript中讀取文本文件,結果取決於瀏覽器
<!DOCTYPE html>
<html>
<head>
<title>reading xml</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Print the contents of the file
var span = document.createElement('span');
span.innerHTML = ['<p>',e.target.result,'</p>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the file
reader.readAsText(f);
// reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
在Firefox的偉大工程。我無法讓它在鉻中工作。我錯過了什麼?!謝謝。
我使用的是Chromium 18.0.1025.168(開發版本的Linux 134367)的Ubuntu 11.10
對我的作品在Chrome 22和15鉻與任何我不得不將到手(對於前者簡單的待辦事項文本文件,JSON後者)。 (http://jsfiddle.net/EYrdE/)。你有沒有嘗試過不同的文本文件?也許你在翻譯中失去了一些東西。你有沒有檢查控制檯的錯誤? –