首页 > 解决方案 > Dropzone 只为最后一个文件构建缩略图

问题描述

我需要将现有文件预加载到 Dropzone-Div 的实例中。我在网上查了一些类似的问题,但没有找到。我想在旅途中创建缩略图,只要只有一个文件,它就可以很好地工作。如果有超过 1 个文件,它只会生成最后一个文件的缩略图。

这是我的 .dropzone 中的初始化函数:

init: function() {      
                // Create the mock files:
                var dropZoneInstance = this;

                // Load Files
                $.ajax({
                   url: 'ajaxcall.php',
                    data: {id: profil,zuordnung: zuordnung, ajaxcall: 'readAnhänge'},
                   success: function (response) {

                    var files = JSON.parse(response);

                    for (i = 0; i < files.length; i++)
                    {
                        var obj = files[i];
                        console.log(obj);

                        dropZoneInstance.files.push(obj);
                        dropZoneInstance.emit("addedfile", obj);

                        dropZoneInstance.createThumbnailFromUrl(obj, 
                            dropZoneInstance.options.thumbnailWidth, 
                            dropZoneInstance.options.thumbnailHeight,
                            dropZoneInstance.options.thumbnailMethod, true, function (thumbnail) 
                            {
                                dropZoneInstance.emit('thumbnail', obj, thumbnail);
                            }
                        );

                        dropZoneInstance.emit("complete", obj);
                    }
                   }
                });
            }

这是 JSON 的 PHP 代码:

        function ReadFromZuordnung($zuordnung = 0)
        {
            $var = Array();

            global $dbh;
            global $user;

            $query = $dbh->prepare("
                SELECT
                    anhang_id AS id
                FROM
                    tbl_anhang
                WHERE
                    anhang_profil = :profil
                AND
                    anhang_zuordnung = :zuordnung
                ");
            $query->bindValue(':profil', $this->getId(), PDO::PARAM_INT);
            $query->bindValue(':zuordnung', $zuordnung, PDO::PARAM_INT);
            $query->setFetchMode(PDO::FETCH_CLASS, 'Test');
            $query->execute();
            while($row = $query->fetch())
            {
                $var[] = Array("id"=>$row->getId(),"name"=>$row->getName(),"size"=>0,"dataURL"=>$this->getUploadPfad().$row->getName());
            }

            echo json_encode($var);
        }

这就是我的test.php:

    <div id="dropzone" class="dropzone" profil="1">
          <div class="dz-default dz-message"></div>
    </div>

这就是结果:https ://imgur.com/9ezsnrk

我想为每个文件都有缩略图,而不仅仅是最后一个。

有什么建议么?

标签: phpjqueryajaxdropzone

解决方案


好的,经过两天的搜索,我自己得到了答案:

只是改变

var obj = files[i];

let obj = files[i];

解决了问题。

当他遇到同样的问题时,也许其他人会节省一些时间。


推荐阅读