1

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

这是我的小提琴

它有效,但是有“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]);
    });
});

有什么想法吗?

谢谢!

4

2 回答 2

6

你可以逃避斜线:

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

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

于 2012-08-10T15:35:31.703 回答
0

如果您的 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

于 2012-08-10T15:40:29.707 回答