doubiaode0460 2019-05-01 13:12
浏览 73

如何使用php for循环创建FilePond实例,并使用插件

I want to use a php for loop to create some file inputs with FilePond.

Each instance needs to have its own unique url, and use the plugins initiated. I have got the actual file uploads working, although the plugins don't seem to be getting used.

<?php
foreach ($objArray as $value) {
    $inputNumber2++;
?>
    <input name="input_<?= $inputNumber2; ?>_images[]" id="input_<?= $inputNumber2; ?>_images" class="filepond" type="files" multiple />
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            window['input<?= $inputNumber2; ?>Images'] = FilePond.create(
                document.querySelector('#input_<?= $inputNumber2; ?>_images'),
                {
                    server: {
                        url: '<?= Config::get('URL'); ?>',
                        process: 'upload/images/input_<?= $inputNumber2; ?>_images',
                        revert: 'upload/revert',
                        load: 'files/',
                    },
                    <?php 
                    if (isset($value->imageArray) && count($value->imageArray) > 0) {
                        echo 'files: [';
                        foreach ($value->imageArray as $imageName) {
                            echo '{
                                source: "'.$imageName.'",
                                options: {
                                    type: "local",
                                    file: {
                                        name: "'.$imageName.'",
                                        type: "image/jpeg"
                                    }
                                }
                            },';
                        }
                        echo '],';
                    }
                    ?>
                }
            );
        });
    </script>
<?php } ?>
...
<script>
document.addEventListener('DOMContentLoaded', function() {
    FilePond.registerPlugin(
        FilePondPluginFileValidateType,
        FilePondPluginImageExifOrientation,
        FilePondPluginImagePreview,
        FilePondPluginImageTransform,
        FilePondPluginImageCrop,
        FilePondPluginImageResize
    );
    FilePond.setOptions({
        acceptedFileTypes: ['image/*'],
        labelIdle: 'Add images or <span class="filepond--label-action"> Browse </span>',
        imagePreviewMaxHeight: 100,
        imagePreviewZoomFactor: 1,

    });
});
</script>

I'm probably doing it wrong - how should I be doing it?

Also, it is picking up and using the custom label so some settings it is getting.

  • 写回答

1条回答 默认 最新

  • dtef9322 2019-05-01 14:19
    关注

    This line:

    document.querySelector('#input_<?= $inputNumber2; ?>_images')
    

    Returns a field with the id input_<?= $inputNumber2; ?>_images. But you've only assigned the name attribute to your input field. I suspect adding the id attribute to the input will solve the issue.

    OR you can change the selector to:

    document.querySelector('input[name="input_<?= $inputNumber2; ?>_images"]')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?