2

我有一个文章发布服务,它有一个带有 uppy.io 的上传表单

一切都很好,但我需要编辑这些文章及其链接图像。

如何将已上传的图像预填充到 uppy.io DashBoard?

我的实际代码:

<div class="DashboardContainer"></div>

<!-- AJAX Uploading for Add Post -->
<script src="https://transloadit.edgly.net/releases/uppy/v1.8.0/uppy.min.js"></script>
<script src="https://transloadit.edgly.net/releases/uppy/locales/v1.11.0/es_ES.min.js"></script>
<script>
    const uppy = Uppy.Core({
            debug: true,
            autoProceed: true,
            restrictions: {
                maxFileSize: 600000,
                maxNumberOfFiles: 10,
                minNumberOfFiles: 1,
                allowedFileTypes: ['.jpg', '.jpeg', '.png', '.gif']
            },
            locale: Uppy.locales.es_ES
        })
        .use(Uppy.Dashboard, {
            inline: true,
            target: '.DashboardContainer',
            replaceTargetContent: true,
            showProgressDetails: true,
            note: 'Sólo imágenes, hasta 10 fotos, de no más de 800kb',
            height: 350,
            width: '100%',
            metaFields: [{
                    id: 'name',
                    name: 'Name',
                    placeholder: 'Nombre del fichero subido'
                },
                {
                    id: 'caption',
                    name: 'Caption',
                    placeholder: 'Describe la imagen que estás subiendo'
                }
            ],
            browserBackButtonClose: true,
            plugins: ['Webcam']
        })
        .use(Uppy.XHRUpload, {
            endpoint: "{{ route('save-image-ajax') }}",
            formData: true,
            fieldName: 'files[]',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
            },
        })
    uppy.on('upload-success', (file, response) => {
        response.status // HTTP status code
        response.body // extracted response data
        // do something with file and response
        $('<input>', {
            type: 'hidden',
            name: 'imageID[]',
            value: response.body.imageID
        }).appendTo("#add");
    })
    uppy.on('complete', result => {
        console.log('successful files:', result.successful)
        console.log('failed files:', result.failed)
    })
</script>

该表单非常适合发布文章,我只想编辑它们,甚至是链接的图像。

4

1 回答 1

3

您可以从 url 预填充图像(首先将远程图像转换为 blob):

fetch({{your_image_url}})
.then((response) => response.blob())
.then((blob) => {
  uppy.addFile({
    name: "image.jpg",
    type: blob.type,
    data: blob
  });
});

然后,将加载图像的状态设置为“已完成”,以避免 Uppy 重新上传它们:

uppy.getFiles().forEach(file => {
  uppy.setFileState(file.id, { 
    progress: { uploadComplete: true, uploadStarted: false } 
  })
})

此外,Uppy 对象配置中的属性“autoProceed”必须为 false。

来源:https ://github.com/transloadit/uppy/issues/1112#issuecomment-432339569

于 2021-01-14T01:34:27.807 回答