Skip to content
Back

Upload Image with unique ID but check file extension

  • 0
  • Web
  • Storage
WuGGu
16 Aug, 2023, 16:44

Hi all,

I'm new to appwrite and bulding my first Nuxt3 Application with appwrite.

I have a form to upload images. I set in my storage policies that only jpg, png and gifs are allowed. For the filename I used ID.unique() to generate unique id from appwrite. But I can't add than the file extension.

Is there a best practice for file upload with the web SDK?

TypeScript
const response: ImageMetadata = await storage.createFile(
        "64be9c5a5a386bde94e2",
        ID.unique(),
        renamedFile,
      );
TL;DR
The user is asking for the best practice for uploading images with a unique ID and checking the file extension. They are also asking for guidance on accessing the file later on a website, whether it should be done by ID or by the file name. One suggestion is to add the file extension to the file name. Others suggest using `ID.unique()` to generate a unique file ID. The user is using the uuidv4 library to generate the unique ID. The user is also trying to rename the file client-side before uploading. It is unclear if the renaming is done client-side or server-side. The user is using the Appwrite web SDK
Drake
16 Aug, 2023, 16:53

Is this client side or server side?

For the filename I used ID.unique() to generate unique id from appwrite

btw, that's the file ID not file name. the file name automatically comes from the file on the web

WuGGu
16 Aug, 2023, 17:01

Upload is on the client side. Yes I renamed also the file for this.

Drake
16 Aug, 2023, 17:03

How are you renaming the file client side?

WuGGu
16 Aug, 2023, 17:17
TypeScript
for (let i = 0; i < imagesToUpload.value.length; i++) {
    const file = imagesToUpload.value[i];
    const fileid = `${uuidv4()}`;
    const renamedFile = new File([file], fileid);

    try {
      const response: ImageMetadata = await storage.createFile(
        "64be9c5a5a386bde94e2",
        fileid,
        renamedFile,
      );

I used uuidv4 as Unique ID library after it doesn't worked with ID.unique() but it feels a little bit odd. This is why I asked for best pratice.

WuGGu
16 Aug, 2023, 17:18

And it only works without storage policy. 😦

Drake
16 Aug, 2023, 17:32

interesting i didn't think you could rename files in the browser...

why do you need to rename it anyways?

WuGGu
16 Aug, 2023, 18:32

because it will be always images taken by smartphones and there could be double ids if it just increment the number in your image roll.

WuGGu
16 Aug, 2023, 18:33

renaming works this way. 🙂

Drake
16 Aug, 2023, 18:54

there's nothing wrong with duplicate file names. Why not use ID.unique() for unique file ids?

Drake
16 Aug, 2023, 18:54

then perhaps you can add the file extension to the file name

WuGGu
16 Aug, 2023, 19:17

and how do I access the file later if I want to display it on a website? By ID or by the file name?

Drake
16 Aug, 2023, 19:20

always by ID

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more