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

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

问题描述

我使用了这个:

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

可以选择文件名,但它返回完整路径,如C:\ fakepath \filename .DOC。 fakepath部分实际上存在 - 不知道是否应该是,但这是我第一次使用文件上传的文件名。



我怎么能获得文件名(filename.doc)?

解决方案

  var filename = $(' [类型=文件]输入 ')VAL()分裂(' \\')POP()。 

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

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


I used this:

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

to get the file name selected, but it returned the full path, as in "C:\fakepath\filename.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.

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

解决方案

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

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天全站免登陆