Get the file name after clicking the open button in the file view dialog using JavaScript / jQuery

I am mostly new to web development and run into a problem with open file dialog box using JavaScript or jQuery . My problem is that I can open the file browser dialog box and after clicking the open button in the file dialog box get the path or name file in alert() .

I am using below code for show dialog box in JavaScript

 <script type="text/javascript"> function performClick(node) { var evt = document.createEvent("MouseEvents"); evt.initEvent("click", true, false); node.dispatchEvent(evt); var pathnew = document.getElementById('theFile').value; } </script> <a href="#" onclick="performClick(document.getElementById('theFile'));">Open file dialog</a> <input type="file" id="theFile" /> 

The above code shows the perfect file view dialog box, but this warning window is displayed when I click on the Open file dialog , but I need to click on the "Open" button, which is located in the "View files" dialog box. Please help me!

Any help would be awarded!

+4
source share
4 answers
 <form> <input type="file"> </form> <script> $(":file").change(function(){ alert($(":file").val()); }); </script> 

put it on jsfiddle for you here: http://jsfiddle.net/9ytkn/

+4
source

Just get value <input type="file" /> .

+1
source

I think this is correct ... Must create a browse button:

 <input id="file_field" type="file"></input> <button type="button" onclick="DisplayFilePath();">Display file path</button> <script> function DisplayFilePath(){ alert($("#file_field").val()) } </script> 
0
source

try the following:

 <SCRIPT> function reverseData(val) {var d="";var temp="";for (var x=val.length;x>0;x--) {d+=val.substring(x,eval(x-1));}return d;} function getFilename(val) {val=escape(val);var reversedsrc=reverseData(val);var nameEnd=reversedsrc.indexOf('C5%');var name=reversedsrc.substring(0,nameEnd);name=reverseData(name);name=unescape(name);return name;} var daname='val'; document.write('<INPUT TYPE="file" ID="val"><INPUT TYPE="button" VALUE="Get File Name" ONCLICK="alert(getFilename(window.document.getElementById(daname).value))">'); </SCRIPT> 

put it in html file and check

0
source

Source: https://habr.com/ru/post/1502829/


All Articles