【问题标题】:Handling File uploads and reorder with Laravel Livewire and Filepond使用 Laravel Livewire 和 Filepond 处理文件上传和重新排序
【发布时间】:2021-06-05 08:29:18
【问题描述】:

我的应用程序中有一个表单,允许用户创建帖子,同时将多张图片上传到正在创建的帖子。

我正在使用 Laravel Livewire 和 Filepond 来实现这一点。

我遇到的问题是我需要允许用户重新排序图像(因为它是一个画廊并且顺序很重要),并在提交表单时将订单保存在数据库中。

我遇到的另一个问题是允许用户稍后编辑他们的帖子。我需要将他们预先存在的帖子图片加载到文件池中,并允许他们上传更多、删除和/或重新排序。

当用户保存帖子时,我需要能够更新我的数据库和文件系统。

几天来我一直在尝试解决这个问题,但没有运气。在线所有信息都是如何上传文件,但没有关于如何重新排序或预填充现有文件的信息。

对此的任何帮助将不胜感激。

这是我目前的参考代码:

<div
    x-data=""
    x-init="
       
        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)
                },
                load: (source, load, error, progress, abort, headers) => {
                    var myRequest = new Request(source);
                    fetch(myRequest).then(function(response) {
                      response.blob().then(function(myBlob) {
                        load(myBlob)
                      });
                    });
                },
            },
        });
        const pond = FilePond.create($refs.input, {
            acceptedFileTypes: ['image/png', 'image/jpeg'],
            maxFileSize: '7MB',
            allowImageCrop: true,
            allowReorder: true,
            allowImageResize: true,
            imageResizeTargetWidth: '1000px',
            imageResizeTargetHeight: '1000px',
            filePosterMaxHeight: '256px',
            files: {{ $existingImages }} // used for when editing a post and it already has images. see php component on how I set this variable
            
        });

    "
>
    <div wire:ignore wire:key="images">
        <div class="form-group text-center">
            <input
                id="image-upload"
                type="file"
                x-ref="input"
                multiple
                data-allow-reorder="true"
                data-max-file-size="3MB"
                data-max-files="10"
            >
        </div>
    </div>
</div>

我的 livewire PHP 组件:

    public $images = [];
    public $existingImages;

    public function mountMedia($post) {
        if($post){
            $this->existingImages = $post->images->map(function ($image) use ($post) {
                return [
                    'source' => $image->id,
                    'options' => [
                        'type' => 'local',
                        'file' => [
                            'name' => $image->getUrl(),
                            'size' => $image->file_size,
                            'type' => $image->mime_type,
                        ],
                        'metadata' => [
                            'poster' => $image->getUrl(),
                            'position' => $image->position
                        ],
                    ],
                ];

            });
        }
    }
    public function saveImage($file, $post, $position) {
        // Create a unique random string
        $randString = Str::random(3);
        // Get time
        $time = time();
        // Set file name
        $filename = $time. '-' . $randString.'-'.auth()->user()->id;
        $extension = '.'.$file->getClientOriginalExtension();

        // Save images for gallery 
        $regImage = $file->storeAs('/'. $post->id, $filename.$extension, 'post_images');

           
        // Create a new image in db
        Image::create([
            'user_id'       => auth()->user()->id,
            'post_id'       => $post->id,
            'position'      => $position,
            'filename'      => $filename,
            'extension'     => $extension,
            'src'           => 'post_images',
            'mime_type'     => $file->getMimeType(),
            'file_size'     => $file->getSize(),
        ]);
    }

    public function saveMedia($post) {
        // Make sure user owns post
        abort_unless($post->user_id == auth()->user()->id, 403);

        // Set default position
        $position = 1;

        // Save each image
        foreach ($this->images as $file) {
           $this->saveImage($file, $post, $position);
            // Increment position for next image
            $position++;
        }
    }
}

【问题讨论】:

  • 对于在这里绊倒的人。我不认为它可以做到。因为文件池中图像的顺序与 livewire 中临时文件数组中图像的顺序不同。临时文件数组是按照图像上传的顺序创建的。文件池顺序是添加图像的顺序。我已经完全放弃了使用 filepond 和 livewire 来重新排序图像。
  • 查看此处了解可能的解决方案:stackoverflow.com/questions/66309775/…

标签: laravel laravel-livewire filepond


【解决方案1】:

对于 Livewire 中的项目排序,我会使用 https://github.com/livewire/sortable。 Sortable 非常易于使用。

对于文件池,如果以后应该再次使用原始图像,我会保存该图像以及与编辑版本的关系。

【讨论】:

  • Filepond 有排序功能。当我进行保存时,我需要知道如何将 filepond 中文件的位置转换为 livewire。我还需要一种方法将以前加载的图像与 livewire 在上传时生成的临时文件结合起来,这样我就可以重新排序和保存
  • 也许这有帮助:youtube.com/watch?v=GRXaCfS1qj0
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2021-05-24
  • 2012-01-18
  • 2012-06-11
  • 2021-03-03
  • 2021-02-03
  • 2013-06-11
相关资源
最近更新 更多