2017-08-31 28 views
0

我正在尝试在文本框中粘贴路径并单击文件读取按钮时读取文件。问题在于它没有读取 - 但是对于带有文件选项的输入标签,它正在读取。从文本框中读取文件时未在输入标记中输入文件类型

你们能告诉我如何实现它吗?在下面提供我的代码。

https://codepen.io/texirv/pen/dzwrqM?editors=1111

<input class="laptop" placeholder="Paste link here to upload document." type="text" name="sportsBox" id="sportsBox"> 

$("#swimmingLink").click(function() { 
     console.log("inside click function--->"); 
      playerValue = $("#sportsBox").val().replace(/\\/g,"/"); 
      var coffee = playerValue.substring(playerValue.lastIndexOf('/')+1) 

      console.log("playerValue--->" + playerValue); 
      console.log("coffee--->" + coffee); 

      //var myReader: FileReader = new FileReader(); 
      var myReader = new FileReader(); 

      //var textType = /text.*/; 
      var file = playerValue.files[0]; 
      console.log("file---->" + file); 
+0

onpaste事件。 https://www.w3schools.com/jsreF/event_onpaste.asp。你正试图在错误的时刻阅读价值。 – PedroSouki

+0

@PedroSouki嘿我试过了,但无法实现在粘贴上使用...你能在我的小提琴更新 –

+1

网页无法以这种方式读取文件。它只能访问用户通过文件输入明确选择的文件,或者从文件浏览器拖放到浏览器上。 –

回答

4

很抱歉,这不能被实现,由于安全方面的原因。

当用户选择自己的文件时,您可以获取文件的唯一方法。

否则,任何人都可以从控制台运行一些简单的脚本,以在您的应用/服务器上传数千个恶意文件。这就是为什么你需要一个类型文件的输入。