Javascript 带有文件名路径的HTML5文件API
我有以下代码通过HTML5文件API读取文件。我已经通过 输入=文件元素。下面是代码块Javascript 带有文件名路径的HTML5文件API,javascript,jquery,html,Javascript,Jquery,Html,我有以下代码通过HTML5文件API读取文件。我已经通过 输入=文件元素。下面是代码块 <input type="file" id="files" name="file" /> <button id="readFile">Read File</button> <output id="content"></output> <script> function readFile() { /* Get the refe
<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>
如果我不想上传文件并通过input=type元素向HTML5:file API提供文件路径来读取并显示文件,该怎么办
我知道HTML5:FileAPI不采用直接文件路径。有什么解决方案吗?出于安全原因,浏览器不允许直接访问Javascript的绝对路径和文件系统。您只能通过在javascript中调用'val'函数来获取文件名,仅此而已
所以不要浪费时间。最新版本的IE会在文本框中返回文件的完整绝对路径。您可能需要使用: var filePath=document.getElementByIdmyFile.value; 获得绝对路径
请注意,仅在IE 11上尝试了的可能副本,因此您仍然无法获取路径,但可以获取文件的内容?伙计们,我不是要求从input=file获取路径。我已经在某个变量中设置了文件路径。我只想使用HTML5:FileAPI的文件路径来读取受尊重的文件,并在某个div或textarea上显示其内容。。。好的,让我对我的问题做一些修改。你不能只读取任何带有HTML5文件API的文件——那将是一个很大的安全漏洞。用户必须自己选择文件。O!是的……你说得对。所以现在别无选择!谢谢你在最新的chrome/firefox/safari中试用过吗?我还能找到路吗?