带文件名路径的HTML5文件API

Ash*_*gde 21 javascript jquery html5

我有以下代码通过HTML5 File API读取文件.我通过input = file element上传了文件.以下是代码块.

<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>
Run Code Online (Sandbox Code Playgroud)

如果我不想上传文件并通过input = type元素将文件路径提供给HTML5:File API来读取文件并显示它会怎么样?

我知道HTML5:File API不采用直接文件路径.有什么解决方案吗?

Maj*_*chi 20

出于安全原因,浏览器不允许直接访问Javascript的绝对路径和文件系统.您只能通过在javascript中调用'val()'函数来获取文件名,仅此而已.

所以不要浪费你的时间.