使用 jQuery 获取文件输入的选定文件名而不带路径 [英] Use jQuery to get the file input's selected filename without the path

查看:22
本文介绍了使用 jQuery 获取文件输入的选定文件名而不带路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过这个:

$('input[type=file]').val()

选择文件名,但返回完整路径,如C:fakepathfilename.doc".fakepath"部分实际上在那里 - 不确定它是否应该存在,但这是我第一次使用文件上传的文件名.

to get the file name selected, but it returned the full path, as in "C:fakepathfilename.doc". The "fakepath" part was actually there - not sure if it's supposed to be, but this is my first time working with the filename of file uploads.

我怎样才能得到文件名(filename.doc)?

How can I just get the file name (filename.doc)?

推荐答案

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

或者你可以这样做(因为它总是 C:fakepath 出于安全原因添加):

or you could just do (because it's always C:fakepath that is added for security reasons):

var filename = $('input[type=file]').val().replace(/C:\fakepath\/i, '')

这篇关于使用 jQuery 获取文件输入的选定文件名而不带路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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