Подтвердить что ты не робот

API файлов HTML5 с именем файла

У меня есть следующий код для чтения файла через 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 не принимает прямой путь к файлу. Есть ли какое-либо решение?

4b9b3361

Ответ 1

По соображениям безопасности браузеры не разрешают доступ к абсолютным путям и файловым системам непосредственно к Javascript. Вы можете получить только имя файла, вызвав функцию val() в javascript и ничего больше.

Так что не тратьте впустую свое время.

Ответ 2

Последняя версия IE возвращает полный абсолютный путь вашего файла в текстовое поле. Вы можете использовать: var filePath = document.getElementById( "myFile" ). value; для получения абсолютного пути

P.S. пробовал только на IE 11