bootstrap 4文件输入不显示文件名

Ter*_*ian 48 html css jquery file-upload bootstrap-4

我在Bootstrap 4中的自定义文件输入类有问题.在我选择了要上传的文件后,文件名不显示.

我用这个代码:

<div class="input-group mb-3">
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile02">
    <label class="custom-file-label" for="inputGroupFile02">Choose file</label>
  </div>
  <div class="input-group-append">
    <button class="btn btn-primary">Upload</button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

知道如何解决它而不会太复杂吗?

deb*_*ebe 60

您需要使用javascript来显示所选文件的名称,如文档中所述:https://v4-alpha.getbootstrap.com/components/forms/#file-browser

在这里您可以找到解决方案:Bootstrap 4文件输入

这是你的例子的代码:

<html lang="en">
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    </head>
    <body>
        <div class="input-group mb-3">
            <div class="custom-file">
                <input type="file" class="custom-file-input" id="inputGroupFile02"/>
                <label class="custom-file-label" for="inputGroupFile02">Choose file</label>
            </div>
            <div class="input-group-append">
                <button class="btn btn-primary">Upload</button>
            </div>
        </div>
        <script>
            $('#inputGroupFile02').on('change',function(){
                //get the file name
                var fileName = $(this).val();
                //replace the "Choose a file" label
                $(this).next('.custom-file-label').html(fileName);
            })
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

  • 你最好像下面的 Anuja 一样使用 `e.target.files[0].name` 。这只是给出名称而没有假路径...... (6认同)
  • 这设置C:\ fakepath\myfile.ext我在linux上看到C:\ ...这不是一个很好的解决方案. (5认同)
  • 您可以操纵字符串以删除“ C \ fakepath”,使其仅包含文件名。var cleanFileName = fileName.replace('C:\\ fakepath \\',“”);` (2认同)
  • @adityaekawade我将其简化为可以将var fileName = $(this).val();替换为var fileName = $(this).val()。replace('C:\\ fakepath \\ ',“”);`听起来合理吗? (2认同)

Anu*_*til 43

我使用了以下相同的内容:

<script type="application/javascript">
    $('input[type="file"]').change(function(e){
        var fileName = e.target.files[0].name;
        $('.custom-file-label').html(fileName);
    });
</script>
Run Code Online (Sandbox Code Playgroud)

  • 如果你有多个文件输入,你可以使用这个:`$(e.target).siblings('.custom-file-label').html(fileName);` (9认同)
  • 这让我成功了90%。但是我有多个文件输入,因此需要按ID而不是类进行定位。 (2认同)
  • 您应该使用“.text(fileName)”而不是“.html(fileName)”。 (2认同)

VDW*_*WWD 19

Anuja Patil的答案仅适用于页面上的单个文件输入.如果有多个,则可以使用.如果multiple已启用,此片段还将显示所有文件.

<script type="text/javascript">

    $('.custom-file input').change(function (e) {
        var files = [];
        for (var i = 0; i < $(this)[0].files.length; i++) {
            files.push($(this)[0].files[i].name);
        }
        $(this).next('.custom-file-label').html(files.join(', '));
    });

</script>
Run Code Online (Sandbox Code Playgroud)

请注意,$(this).next('.custom-file-label').html($(this)[0].files.join(', '));这不起作用.这就是为什么for需要循环的原因.

如果您从未在文件上传中使用多个文件,则可以使用这个更简单的代码段.

<script type="text/javascript">

    $('.custom-file input').change(function (e) {
        $(this).next('.custom-file-label').html(e.target.files[0].name);
    });

</script>
Run Code Online (Sandbox Code Playgroud)

  • 如果有人取消文件上传,“e.target.files”将为空并抛出错误“Uncaught TypeError:无法读取未定义的属性“名称””。如果您检查“e.target.files”的长度,则可以避免控制台错误。虽然功能上没有任何问题。 (2认同)
  • @phyatt,找到它并修复它。感谢您指出了这一点! (2认同)

kej*_*ion 11

$(document).on('change', '.custom-file-input', function (event) {
    $(this).next('.custom-file-label').html(event.target.files[0].name);
})
Run Code Online (Sandbox Code Playgroud)

世界上最好的。适用于动态创建的输入,并使用实际的文件名。


kex*_*eam 6

这适用于Bootstrap 4.1.3:

<script>
    $("input[type=file]").change(function () {
        var fieldVal = $(this).val();

        // Change the node's value by removing the fake path (Chrome)
        fieldVal = fieldVal.replace("C:\\fakepath\\", "");

        if (fieldVal != undefined || fieldVal != "") {
            $(this).next(".custom-file-label").attr('data-content', fieldVal);
            $(this).next(".custom-file-label").text(fieldVal);
        }
    });
</script>
Run Code Online (Sandbox Code Playgroud)


小智 6

Johann-S 解决方案效果很好。(看起来他创建了很棒的插件)

Bootstrap 4.3文档示例页面使用此插件来修改文件名:https://github.com/Johann-S/bs-custom-file-input

使用Bootstrap 自定义文件输入类。将插件添加到您的项目中,并将此代码添加到页面上的脚本文件中。

$(document).ready(function () {
  bsCustomFileInput.init()
})
Run Code Online (Sandbox Code Playgroud)


Joh*_*n-S 5

如果你愿意,你可以使用推荐的 Bootstrap 插件来动态化你的自定义文件输入:https : //www.npmjs.com/package/bs-custom-file-input

这个插件可以在有或没有 jQuery 的情况下使用,并且可以与 React 和 Angular 一起使用