У меня есть следующий код для чтения файла через API файлов HTML5. Я загрузил файл через input = элемент файла. Ниже приведен код.
<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 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>
Что делать, если я не хочу загружать файл и предоставлять путь к файлу через элемент input = type в HTML5: API файлов для чтения файла и отображения его?
Я знаю, что API HTML5: File API не принимает прямой путь к файлу. Есть ли какое-либо решение?