繁体   English   中英

使用带有“ split”的jQuery警报输入“文件”文件名

[英]Alert input “file” filename using jQuery with “split”

我正在尝试从上传输入中提醒文件名。

这是我的小提琴

它可以工作,但是有类似的“ C:Fakepath ...”。 我只想要文件名,没有伪路径。 我尝试使用split函数,但不确定为什么它不起作用。

范例html:

<html>
    <head>
        <title>Test Page</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <input type="file" id="tester" />
    </body>
</html>

示例脚本:

$(function() {
    var bogus;
    var triple;
    $('#tester').change(function() {

        triple = $('#tester').val();
        bogus = triple.split(/[\s/]+/);

        alert(bogus[bogus.length - 1]);
    });
});

有什么想法吗?

谢谢!

您可以逃脱斜线:

   bogus = triple.split("\\");

更新的小提琴: http : //jsfiddle.net/johnkoer/xwdct/3/

如果您的Web浏览器支持File API (当前是不是IE的任何Web浏览器),则可以从输入的FileList对象获取文件名。

$(function() {
    $('#tester').change(function() {
        var files = this.files;
        if (files && files.length) {
            alert(files[0].name); //use the file api
        }
        else {
            alert($(this).val().replace("C:\\fakepath\\", ""));  //this is for IE
        }
    });
});​

示例: http//jsfiddle.net/NTICompass/xwdct/6/

文件: https//developer.mozilla.org/en-US/docs/Using_files_from_web_applications

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM