Tôi có mã sau để đọc tệp thông qua API tệp HTML5. Tôi đã tải lên tệp qua đầu vào = phần tử tệp. Sau đây là khối mã.API tệp HTML5 với đường dẫn tên tệp
<input type="file" id="files" name="file" />
<button id="readFile">Read File</button>
<output id="content"></output>
<script>
function readFile()
{
/* Get the reference of the inpout element. */
var files = document.getElementById('files').files;
console.log(files);
if (!files.length)
{
alert('Please select a file!');
return;
}
/* Reading the first file selected. You can process other files similarly in loop. */
var file = files[0];
/* Instantiate the File Reader object. */
var reader = new FileReader();
/* onLoad event is fired when the load completes. */
reader.onload = function(event) {
document.getElementById('content').textContent = event.target.result;
};
/* The readAsText method will read the file's data as a text string. By default the string is decoded as 'UTF-8'. */
reader.readAsText(file);
}
document.getElementById('readFile').addEventListener('click', function(event) {
readFile();
}, false);
</script>
Điều gì xảy ra nếu tôi không muốn tải lên tệp và cung cấp filepath qua phần tử input = type tới HTML5: File API để đọc tệp và hiển thị?
Tôi biết rằng HTML5: API tệp không có đường dẫn tệp trực tiếp. Có giải pháp nào không?
bản sao có thể có của [Cách lấy đường dẫn đầy đủ của tệp từ đầu vào tệp] (http://stackoverflow.com/questions/4176377/how-to-get-the-full-path -of-the-file-from-a-file-input) – Quentin
Vì vậy, bạn vẫn không thể nhận được đường dẫn nhưng bạn có thể nhận được nội dung của tệp? – HMR
Các bạn, tôi không yêu cầu lấy đường dẫn từ tệp input =. Tôi đã có đường dẫn tệp trong một số biến. Tôi chỉ muốn sử dụng đường dẫn tệp đó với HTML5: API tệp để đọc tệp được tôn trọng đó và hiển thị nội dung của nó trên một số div hoặc văn bản ... ok Hãy để tôi thực hiện một vài thay đổi đối với Câu hỏi của tôi. –