【问题标题】:Pass dropzone uploaded filename to hidden input field将 dropzone 上传的文件名传递给隐藏的输入字段
【发布时间】:2018-10-01 19:13:09
【问题描述】:

我在一个页面中有 2 个表单,第一个是:dropzone,第二个是:用户需要填写的表单。我想当用户在 dropzone 中上传文件时,它将以第二种形式添加隐藏输入。这是我的表格

<form action="/users/upload.php" class="dropzone needsclick dz-clickable" id="poster"></form>
 <form action='' method='post' name='create' enctype="multipart/form-data">
<div id="ideaform"></div>
</form>

这是我的upload.php

    <?php
if (!empty($_FILES)) {

    $tempFile = $_FILES['file']['tmp_name'];       
    $name = $_FILES['file']['name']; 
    $x = explode('.', $name);
    $ext = strtolower(end($x));
    $name = md5(time().$name).'.'.$ext;
    move_uploaded_file($tempFile, 'images/uploaded/'.$name);
}
?>

我在互联网上探索了这个方法来获取一个文件名并将它放在隐藏输入上的值并找到这个 jQuery,每当我尝试上传文件时,隐藏输入上的值总是“未定义”

 Dropzone.options.poster = {
                maxFiles:1,
                acceptedFiles: "image/*",
                init: function() {
                      this.on("maxfilesexceeded", function(file) {
                            this.removeAllFiles();
                            this.addFile(file);
                      });
                    this.on('success', function(file, response) {
                    $("#ideaform").append($('<input type="hidden" ' + 'name="files" ' + 'value="' + response.fileName + '">'));
                    });
                }
            };

我哪里出错了? 非常感谢。

【问题讨论】:

    标签: php jquery dropzone


    【解决方案1】:

    我找到的解决方案是使用 JSON。代码如下:

    Dropzone.options.poster = {
                    acceptedFiles: "image/*",
                    maxFilesize: 5, // MB
                    maxFiles: 1,
                    init: function() {
                          this.on("maxfilesexceeded", function(file) {
                                this.removeAllFiles();
                                this.addFile(file);
                          });
    
                        this.on('success', function(file, response) {
                        JSON.parse(response);
                        console.log(response);
                        $("#ideaform").append($('<input type="hidden" name="file"  value='+response+'>'));
                        });
                    }
                };
    

    对于表单操作,我得到了这个

        <?php
    if (!empty($_FILES)) {
    
        $tempFile = $_FILES['file']['tmp_name'];       
        $name = $_FILES['file']['name']; 
        $x = explode('.', $name);
        $ext = strtolower(end($x));
        $name = md5(time().$name).'.'.$ext;
        move_uploaded_file($tempFile, 'images/uploaded/'.$name);
        $ar = $name;
        echo json_encode($ar);
    }
    ?>
    

    【讨论】:

    • 令人沮丧 - 您的代码看起来不错,但我收到的响应总是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2021-01-31
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多