简体   繁体   English

隐藏文件上传按钮但显示文件名?

[英]Hide file upload button but show file names?

I made a simple drag and drop zone for multiple file uploads.我为多个文件上传制作了一个简单的拖放区。 I want the user to just drag their files into the drop zone and have the file names be displayed there.我希望用户只需将他们的文件拖到放置区域并在那里显示文件名。 I hid the standard "Choose file" button by setting 'display: none' in CSS.我通过在 CSS 中设置“显示:无”隐藏了标准的“选择文件”按钮。 But that also hides the file names.但这也隐藏了文件名。 Is there a simple way to fix this?有没有简单的方法来解决这个问题? I'm a beginner.我是初学者。 Thank you!谢谢!

HTML: HTML:

         <div class="drop-area">
            <span class= "drop-area__prompt">Dateien hier her ziehen</span>
            <input type="file" name="myFile" class= "drop-area__input">
        </div>

CSS: CSS:

.drop-area__input{ 
    display:none;
}

It seems to me, there is no way to do it without using JS.在我看来,不使用 JS 就没有办法做到这一点。 In my example, file upload triggered by the click on emoji.在我的示例中,通过单击表情符号触发文件上传。 In your case, you have to put it in the method where you handle drag&drop.在您的情况下,您必须将其放入处理拖放的方法中。

 let fileUpload = document.getElementById('fileUpload'); let fileName = document.getElementById('fileName'); fileUpload.addEventListener('change', function(){ if(this.files.length) fileName.innerText = this.files[0].name; else fileName.innerText = ''; });
 <div class="drop-area"> <span class= "drop-area__prompt">Dateien hier her ziehen</span> <span id="fileName"></span> <label> <img height="30" src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/237/outbox-tray_1f4e4.png"/> <input id="fileUpload" type="file" name="myFile" class= "drop-area__input" style="display:none;"> </label> </div>

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

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