stratboy's avatar

How to load initial images in Filepond?

Hi, I've read the docs and several tuts and threads. Also asked in SO and received no answer.

I managed to use filepond to load images in Amazon S3. The bucket has public read access (already verified direct browser access). Now I need to show the loaded images in my laravel model's edit page.

I'm using the filepond's 'file' property, and passing data like this, but it doesn't work:

// [amazon server obscured for the case]
[
  {
    "source": "https://[amazon server].amazonaws.com/images/XJlC4lBZJ2KDmp8f7oC.png",
    "options": {  "type": "local" }
  }
]

It's not clear to me what else I should do.

It's also not really clear if I also have to use the server.load property and eventually how (both client and server side). Filepond seems to be a great library but really lacks of working examples and templates in docs.

0 likes
8 replies
LaryAI's avatar
Level 58

To load initial images in FilePond, you need to properly configure the files property of the FilePond instance. The source should be the URL of the image, and you need to specify the type as local. Additionally, you should ensure that FilePond is properly initialized and the files are correctly passed to it.

Here's a step-by-step solution:

  1. Include FilePond CSS and JS: Make sure you have included the necessary FilePond CSS and JS files in your HTML.

  2. Initialize FilePond: Initialize FilePond on your file input element.

  3. Set Initial Files: Use the files property to set the initial files.

Here is an example of how you can achieve this:

HTML

<!-- Include FilePond styles -->
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">

<!-- File input element -->
<input type="file" class="filepond" name="filepond" />

<!-- Include FilePond library -->
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>

JavaScript

// Initialize FilePond
FilePond.registerPlugin();

// Select the file input element
const inputElement = document.querySelector('input[type="file"]');

// Create a FilePond instance
const pond = FilePond.create(inputElement);

// Set initial files
pond.files = [
  {
    source: 'https://[amazon server].amazonaws.com/images/XJlC4lBZJ2KDmp8f7oC.png',
    options: {
      type: 'local'
    }
  }
];

Laravel Blade Template

If you are using Laravel Blade templates, you can embed the above HTML and JavaScript within your Blade file:

@extends('layouts.app')

@section('content')
<div class="container">
    <input type="file" class="filepond" name="filepond" />
</div>
@endsection

@section('scripts')
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
<script>
    // Initialize FilePond
    FilePond.registerPlugin();

    // Select the file input element
    const inputElement = document.querySelector('input[type="file"]');

    // Create a FilePond instance
    const pond = FilePond.create(inputElement);

    // Set initial files
    pond.files = [
      {
        source: '{{ $imageUrl }}', // Pass the image URL from your controller
        options: {
          type: 'local'
        }
      }
    ];
</script>
@endsection

Controller

In your Laravel controller, pass the image URL to the view:

public function edit($id)
{
    $model = YourModel::find($id);
    $imageUrl = $model->image_url; // Assuming you have an image_url attribute

    return view('your-view', compact('imageUrl'));
}

This setup should correctly load the initial images in FilePond from your Amazon S3 bucket. Make sure the URL is publicly accessible and correctly formatted.

stratboy's avatar

Lary's answer seems not totally correct, for some reason filepond still isn't loading the image, without any error logged in console.. In Amazon S3 I setted the following CORS:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "[>> app domain <<]"
        ],
        "ExposeHeaders": []
    }
]
Snapey's avatar

You should look in the network tab of your browser dev tools and see if the browser is trying to load the image.

If it fails it will be in red and you should see a reason.

stratboy's avatar
stratboy
OP
Best Answer
Level 5

I'm replying for myself.

First, since I'm using S3, I needed to update the CORS policy and add GET:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
			"GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "[>> app domain <<]"
        ],
        "ExposeHeaders": []
    }
]

Then I needed to add a server.load prop to the filepond config:

    load: (source, load) => {
      // source here comes from the files array, see my first post
      fetch(source).then(res => res.blob()).then(load);
    },

To me, was quite counterintuitive to add a load function since I'm passing direct urls via the files array. Instead, it is required. Filepond's docs are not clear at all, they would at least need some more working examples and use cases. For example, I've found out that I can even assign a direct url to the load prop, thus not a function. The source prop in files definitions would be appendeded to the load url in that case. So source could be for example a resource ID instead of an url, in this very case. This is not specified at all on the fileponds docs.

Anyway, I came up with the above solution, that is working fine. Obviously enough, I'm populating the files array with dynamic data, not hand written urls.

cjholowatyj's avatar

@stratboy I didn't need to change anything with CORS, but I can confirm that this worked for me also!!! Thank you so much!

I was having difficulty implementing this myself because I also use ImgIX with signed images (and I locked down my S3 buckets disabling public access), so being able to manually handle the url of an existing image and inject it into the FilePond component was incredibly helpful!

1 like

Please or to participate in this conversation.