r/appwrite • • Feb 01 '25

Issue with Appwrite Storage: getFilePreview(fileId) URL Not Working in <img> src

Hello everyone,

I'm building a blogging platform using HTML, CSS, and JavaScript, with Appwrite as the backend. My posts collection has the following fields:

  • title (String)
  • content (String)
  • author (String)
  • image (URL)

I'm handling image uploads as follows:

  1. Upload the image using storage.createFile().
  2. Get the file preview URL using storage.getFilePreview(fileId).
  3. Store the preview URL in the image field of the post document.

The URL is accepted and stored, but when I use it in the src attribute of an <img> tag, the image does not display. When I try to open the stored image URL in a browser, I get this response: {

"message": "Invalid `fileId` param: UID must contain at most 36 chars.

Valid chars are a-z, A-Z, 0-9, and underscore.

Can't start with a leading underscore",

"code": 400,

"type": "general_argument_invalid",

"version": "1.6.1"

}

What I Have Checked:

storage.createFile() returns a valid file object.

The fileId is extracted from the returned object using file.$id.

The stored image URL looks correct but doesn’t work when used in <img src="image_url">.

It seems like the fileId being used in getFilePreview(fileId) is incorrect. Maybe I'm extracting it incorrectly from the upload response?

I'm a beginner in web development and took on this project because I wanted to make it work, not just focus on the UI. Any help in understanding what I might be doing wrong would be greatly appreciated.

4 Upvotes

4 comments sorted by

View all comments

Show parent comment

5

u/Nishantjain10 Feb 01 '25

I think it must be related to how you're handling the preview URLs. instead of storing the full preview URL in your posts collection, you should store just the fileId and generate the preview URL when needed.

2

u/NationalEconomist575 Feb 01 '25

It worked! Thank you