使用 JavaScript/jQuery 在文件浏览对话框中单击打开按钮后获取文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18874729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the file name after click open button in file browse dialog box using JavaScript/jQuery
提问by Jay Shukla
Basically I am new in web development and I'm facing a problem related to open file dialog boxusing JavaScriptor jQuerymy problem is that how can I open a file browser dialog boxand after clicking openbutton in the file dialog boxget the file pathor namein alert()method.
基本上,我在Web开发新的和我现在面临打开相关的问题时file dialog box使用JavaScript或jQuery我的问题是,我怎么能打开file browser dialog box和点击后open的按钮file dialog box获取文件path或者name在alert()方法。
I am using below code for show dialog box in JavaScript
我正在使用以下代码在 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" />
above code shows perfectly file browse dialog box but this alert box show when I click on
Open file dialogbut I need after clicking Open button which is under the File browse dialog box.
Please Help me!
上面的代码完美地显示了文件浏览对话框,但是当我点击时会显示这个警告框,
Open file dialog但我需要点击文件浏览对话框下的打开按钮。请帮我!
Any help will be appropriated!
任何帮助将被挪用!
采纳答案by GrahamJRoy
<form>
<input type="file">
</form>
<script>
$(":file").change(function(){
alert($(":file").val());
});
</script>
put it on jsfiddle for you here: http://jsfiddle.net/9ytkn/
把它放在 jsfiddle 上给你:http: //jsfiddle.net/9ytkn/
回答by SLaks
Just get the valueof the <input type="file" />.
刚刚拿到value的<input type="file" />。
回答by Mentok
I think this is right...Should produce 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>
回答by Ahsan Shah
try this:
尝试这个:
<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 into html file and test
把它放入html文件并测试

