【发布时间】:2021-05-24 08:17:40
【问题描述】:
我有一个 Filepond 组件,非常适合将图像上传到帖子。
但是,当我需要编辑帖子及其预先存在的图像时,我在使用 Filepond 时遇到了困难。
我想用 Post 模型的预先存在的图像加载 Filepond。目标是允许用户在编辑 Post 模型时上传、删除和重新排序图像,然后更新数据库和文件系统。
这是我目前所拥有的:
<div
x-data=""
x-init="
// Plugins
FilePond.registerPlugin(FilePondPluginImagePreview);
FilePond.registerPlugin(FilePondPluginImageExifOrientation);
FilePond.registerPlugin(FilePondPluginFileValidateType);
FilePond.registerPlugin(FilePondPluginFileValidateSize);
FilePond.registerPlugin(FilePondPluginImageResize);
// Set options
FilePond.setOptions({
allowMultiple: true,
allowReorder: true,
itemInsertLocation: 'after',
server: {
process: (fieldName, file, metadata, load, error, progress, abort, transfer, options) => {
@this.upload('images', file, load, error, progress)
},
revert: (filename, load) => {
@this.removeUpload('images', filename, load)
},
},
// This allows me to reorder the images, essential resetting the $image variable in livewire component when ever th euser reorders the images in filepond
onreorderfiles(files, origin, target){
@this.set('images', null);
files.forEach(function(file) {
@this.upload('images', file.file);
});
},
});
// Create Filepond
const pond = FilePond.create($refs.input, {
acceptedFileTypes: ['image/png', 'image/jpeg'],
@if(optional($post)->images) // If we are editing a post and if that post has images
files: [
@foreach($post->images as $image)// Loop through each image for the post
{
// the server file reference
source: '{{ Storage::disk('post_images')->url($post->id . '/' . $image->filename.$image->extension) }}',
},
@endforeach
],
@endif
});
pond.on('addfile', (error, file) => {
if (error) {
console.log('Oh no');
return;
}
});
"
>
<div wire:ignore wire:key="images">
<input
id="image-upload"
type="file"
x-ref="input"
multiple
>
@error('images.*')
<p wire:key="error_images" class="mt-2 text-sm text-red-600" id="email-error">{{ $message }}</p>
@enderror
</div>
</div>
但是,这会导致每次用户查看编辑页面时重新上传所有图像。
网上有很多关于如何使用 filepond 和 livewire 上传图片的信息,但关于如何编辑的内容并不多。
除了每次用户查看编辑页面时重新上传之外,还有更好的方法来管理图像吗?
对此的任何帮助将不胜感激。
【问题讨论】:
-
对于在这里绊倒的人。我不认为它可以做到。因为文件池中图像的顺序与 livewire 中临时文件数组中图像的顺序不同。临时文件数组是按照图像上传的顺序创建的。文件池顺序是添加图像的顺序。我已经完全放弃了使用 filepond 和 livewire 来重新排序图像。
标签: laravel laravel-livewire filepond