如何使用jQuery从上传表单获取文件名? [英] How to Get File Name from Upload Form Using jQuery?

查看:188
本文介绍了如何使用jQuery从上传表单获取文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery获取上载文件的文件名.但是问题是,我得到了伪造的路径和文件名,而不是仅文件名.这是我的表格.

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>

jQuery就是这样

And the jQuery is like this

$(document).ready(function(){
    $('#inputSubmit').click(function(){
      alert($('#uploadFile').val());
    });
});

我使用Chrome,并将其显示在警报框中.假设我选择的文件名为filename.jpg.

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.

推荐答案

用"\"分隔文件路径字符串,并使用结果数组中的最后一项.

Split the filepath string with "\" and use the last item in the resulting array.

请参阅:获取以下内容的最后一个元素分割字符串数组

这篇关于如何使用jQuery从上传表单获取文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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