硬编码文件路径,而不是手动文件输入 [英] Hardcode file path instead of manual file input

查看:77
本文介绍了硬编码文件路径,而不是手动文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Javascript读取文本文件.为了传递 FileList对象,可用代码将< input type ="file"> 与change事件一起使用,但是我希望txt文件的路径为在javascript中进行硬编码,并在文档加载时触发该事件.我该如何实施呢?带有< input type ="file"> 的可用代码为:

I want to read a text file using Javascript. For passing the FileList Object, the available codes use the <input type="file"> with change event, but i want the path of the txt file to be hardcoded inside javascript and the event to be fired onload of document. How can I implement it?? The available code with <input type="file"> is:

<input type="file" id="fileinput" />
<script type="text/javascript">
  function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 
    console.log(evt);
    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;
        alert( "Got the file."+f);  
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }
  document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>

推荐答案

您不能.访问用户系统上的文件要求用户明确选择它们以作为一种安全措施.

You can't. Access to files on the user's system requires that the user picks them explicitly as a security measure.

这篇关于硬编码文件路径,而不是手动文件输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆