简体   繁体   English

上传后如何用javascript显示文件名

[英]How to display file name with javascript after upload

I want to use a different button to upload files to a form.我想使用不同的按钮将文件上传到表单。 Therefore, i hide the standard upload file button.因此,我隐藏了标准的上传文件按钮。 However, i do want to display the filename when a user uploads a file.但是,我确实想在用户上传文件时显示文件名。

Using wordpress contact form 7, i already tried putting a JS function on the label, but that doesnt work.使用 wordpress contact form 7,我已经尝试在标签上放置一个 JS 函数,但这不起作用。

<label for="fileInput" class="custom-file-upload" onclick="displayfilename()">Choose file</label>
<input id="fileInput" name="fileInput" type="file" />
<span class="fileuploadspan">No file selected</span>

<script>
function displayfilename() 
$('#fileInput').change(function(){
var filename = $(this).val().split('\\').pop();
});
</script>

The filename should be displayed next to the label.文件名应显示在标签旁边。

You can use Event.target along with triggering the change event.您可以使用Event.target以及触发更改事件。

Please Note: You also have syntax error in your code (missing the { ....... } part of the function displayfilename ).请注意:您的代码中也有语法错误(缺少函数displayfilename{ ....... }部分)。

 $('#fileInput').change(function(e){ var filename = e.target.files[0].name; console.log(filename); }); function displayfilename() { $('#fileInput').trigger('change'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="fileInput" class="custom-file-upload" >Choose file</label> <input id="fileInput" name="fileInput" type="file" /> <span class="fileuploadspan">No file selected</span>

OR: You can also use this object:或者:您也可以使用对象:

 $('#fileInput').change(function(){ var filename = $(this)[0].files[0].name; console.log(filename); }); function displayfilename() { $('#fileInput').trigger('change'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="fileInput" class="custom-file-upload" >Choose file</label> <input id="fileInput" name="fileInput" type="file" /> <span class="fileuploadspan">No file selected</span>

 $('#fileInput').change(function(e){ var filename = e.target.files[0].name; console.log(filename); }); function displayfilename() { $('#fileInput').trigger('change'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="fileInput" class="custom-file-upload" >Choose file</label> <input id="fileInput" name="fileInput" type="file" /> <span class="fileuploadspan">No file selected</span>

You can access the file name in this way:您可以通过这种方式访问文件名:

<label for="fileInput" class="custom-file-upload" onclick="displayfilename()">Choose file</label>
<input id="fileInput" name="fileInput" type="file" />
<span class="fileuploadspan">No file selected</span>

<script>
function displayfilename() 
$('#fileInput').change(function(e) {
  var fileName = e.target.files[0].name;
  alert('The file "' + fileName +  '" has been selected.');
});
</script>

Here's a custom <button> and a custom filename <span> :这是一个自定义<button>和一个自定义文件名<span>

 $('.choosefile').click(function () { $('#fileInput').click(); }); $('#fileInput').change(function(e) { var filename = this.files[0].name; $('.fileuploadspan').text(filename); });
 input[type=file] { display: none }.choosefile, .fileuploadspan { font-family: "Comic Sans MS" }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="fileInput" class="custom-file-upload">Choose file</label> <button class="choosefile">Browse...</button> <input id="fileInput" name="fileInput" type="file" /> <span class="fileuploadspan">No file selected</span>

    <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <meta http-equiv="X-UA-Compatible" content="ie=edge">
       <title>Document</title>
   </head>
   <body>

    <label for="fileInput" class="custom-file-upload">Choose file
            <input id="fileInput" name="fileInput" type="file"  onchange="uploadPreview(this)"  style="display:none"/>
         </label><br>
         <span class="fileuploadspan">No file selected</span>
   </body>
   </html>

js js

uploadPreview = function (fileInput) {
        var files = fileInput.files;
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var imageType = /image.*/;
            if (!file.type.match(imageType)) {
                continue;
            }
            var reader = new FileReader();
            reader.onload = function (e) {

                var filename = file.name;
                filename = filename.replace('.jpg', '');
                filename = filename.replace('.jpeg', '');
                filename = filename.replace('.png', '');
                filename = filename.replace('.gif', '');
                filename = filename.replace('.bmp', '');
                $('.fileuploadspan').text(filename);

                return;
            };
            reader.readAsDataURL(file);
        }
    }

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

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