douting0585 2016-06-15 20:13
浏览 146

在上传表单中显示所选文件的名称

I want to show the selected filename and extension as seen on picture below. All this takes place inside a HTML form.

upload

The best code that i have found for this purpose is this (also what i shown on picture):

<label for="uploaded_file">Vælg en fil:</label>
<input type="file" name="uploaded_file">
<p>Valgt fil:</p>
<?php
    if(isset($_FILES['uploaded_file']['name'])) {
        echo $_FILES['uploaded_file']['name']." was uploaded";
    }else{
        echo "No File Uploaded";
    }
?>

But this code doesn't show it. And ofcourse it doesn't because i haven't submitted the form yet. Also i have been looking for som jQuery/Javascript features, but i have found none that fit or could fit my purpose.

My whole intension is later on i will "upgrade" my upload form for upload of multiple files.

How can i make this script working?

  • 写回答

2条回答 默认 最新

  • doujia1163 2016-06-15 20:18
    关注

    You can get the name of the uploaded file by grabbing the input off the DOM with javascript, and then accessing the value property. Note, the input must have the file already selected by the user, so in the example below I use a <button> click handler to check the value.

    HTML:

    <label for="uploaded_file">Vælg en fil:</label>
    <input id="file" type="file" name="uploaded_file">
    <p>Valgt fil:</p>
    
    <button id="btn">Get File Name</button>
    

    JavaScript:

    var input = document.getElementById('file');
    
    // Attach click handler to button.
    document.getElementById('btn').addEventListener('click', function() {
        // Grab name of uploaded file (use the `value` property of the input element).
        alert(input.value);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动