如何在页面加载时在多个文件输入字段中设置文件?

时间:2019-12-23 08:01:53

标签: javascript php html forms

我正在使用个人资料更新功能,其中用户要显示多个图像,以便他可以添加新图像或删除旧图像,我正在使用js库,通过该库显示预览,但是当我提交表单时,该数据中不存在旧图像。我可以将值推入文件输入字段,还是可以解决此问题,以便实现此功能。

Link to Library that I'm using

我的Js代码:

var upload = new FileUploadWithPreview('myUniqueUploadId', {
    showDeleteButtonOnImages: true,
    text: {
        chooseFile: 'Įkelkite nuotrauką',
        browse: 'naršyti',
        selectedCount: 'Pasirinkti failai',
    },
    presetFiles: [
        <?php
            foreach($Workimages as $Workimg){
                echo $website_url.'master_image/'.$Workimg.','
        }?>
    ],
});

1 个答案:

答案 0 :(得分:0)

您缺少引用图像路径的提示

var upload = new FileUploadWithPreview('myUniqueUploadId', {
    showDeleteButtonOnImages: true,
    text: {
        chooseFile: 'Įkelkite nuotrauką',
        browse: 'naršyti',
        selectedCount: 'Pasirinkti failai',
    },
    presetFiles: [
        <?php
            foreach($Workimages as $Workimg){
                echo '"'.$website_url.'master_image/'.$Workimg.'",'
        }?>
    ],
});

更好的方法是使用json_encode数组

<?php 
 $Workimages = array_map(function($v) use($website_url) {  return $website_url.'master_image/'.$Workimg;  }, $Workimages);
?>
var upload = new FileUploadWithPreview('myUniqueUploadId', {
    showDeleteButtonOnImages: true,
    text: {
        chooseFile: 'Įkelkite nuotrauką',
        browse: 'naršyti',
        selectedCount: 'Pasirinkti failai',
    },
    presetFiles: <?= json_encode($Workimages); >
});