带文件名路径的HTML5文件API

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

    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);  

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

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

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

所以不要浪费你的时间。

最新版本的IE将在文本框中返回文件的完整绝对路径。 您可能想要使用:var filePath = document.getElementById(“myFile”)。value; 获得绝对路径

PS仅在IE 11上尝试过