如何使用 jQuery 从上传表单中获取文件名?

发布于 2024-12-05 14:52:45 字数 584 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入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

晒暮凉 2024-12-12 14:52:45

您还可以执行以下操作:

$('#uploadFile').prop("files")['name'];

如果是多个文件输入,请执行以下操作:

$.each($('#uploadFile').prop("files"), function(k,v){
    var filename = v['name'];

    // filename = "blahblah.jpg", without path
});

You can also do:

$('#uploadFile').prop("files")['name'];

If it's a multiple file input, do:

$.each($('#uploadFile').prop("files"), function(k,v){
    var filename = v['name'];

    // filename = "blahblah.jpg", without path
});
喜你已久 2024-12-12 14:52:45

使用jquery上传时获取文件名

var filename = $('input[type=file]').val().split('\').pop();

get file name when uploading using jquery

var filename = $('input[type=file]').val().split('\').pop();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文