如何使用 jQuery 从上传表单中获取文件名?
我想使用 jQuery 获取上传文件的文件名。但问题是,我得到了假路径和文件名,而不仅仅是文件名。这是我的表格。
<form action="" method="post">
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" name="submit" value="Upload" id="inputSubmit" />
</form>
jQuery 就像这样
$(document).ready(function(){
$('#inputSubmit').click(function(){
alert($('#uploadFile').val());
});
});
我使用 Chrome 并在警报框中得到了这个。假设我选择名为 filename.jpg 的文件。
C:\fakepath\filename.jpg
如何只获取文件名?感谢您的帮助。
I want to get the file name of the uploaded file using jQuery. But the problem, I got the fake path and file name, instead of the file name only. This is my form.
<form action="" method="post">
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" name="submit" value="Upload" id="inputSubmit" />
</form>
And the jQuery is like this
$(document).ready(function(){
$('#inputSubmit').click(function(){
alert($('#uploadFile').val());
});
});
I use Chrome and got this in the alert box. Let's say I choose file named filename.jpg.
C:\fakepath\filename.jpg
How can I get only the file name? Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用“\”分割文件路径字符串并使用结果数组中的最后一项。
请参阅: 获取分割字符串的最后一个元素数组
Split the filepath string with "\" and use the last item in the resulting array.
see: Getting the last element of a split string array
您还可以执行以下操作:
如果是多个文件输入,请执行以下操作:
You can also do:
If it's a multiple file input, do:
使用jquery上传时获取文件名
var filename = $('input[type=file]').val().split('\').pop();
get file name when uploading using jquery
var filename = $('input[type=file]').val().split('\').pop();