Change publicID based on upload file name
hello, I am trying to use the preBatch hook to rename the publicId before uploading. (e.g: filename "test.png" becomes "110020_test.png" where 110020 is my application supplied user ID).
however, I could not find information about the usage of "cb" and "data" parameters.
i have already tried to change the publicId field itself on the "data" data structure (eg: data.files[0].publicId = myUserId + "_" + data.files[0].name) to no avail. the file gets a random name
my upload preset - type=unsigned, use filename=OFF, disallow public ID =OFF, discard original filename=OFF
-
Hi Marcel,
I'm afraid it isn't possible to use the preBatch method in this way - it provides a hook to allow you to perform your application's own logic on the files before they're uploaded, but you can't set the public_id in the way you're describing here.
For the use-case you've mentioned, I would recommend one of the following methods instead:
- Use tags or context values on the upload to identify the user who uploaded the file
- Accept the file from the user in javascript directly, then call the API using your own code to specify the public_id, folder, etc
- In conjunction with tags or context as above, or via logging the Upload Widget response yourself, have us notify your server-side code when the file upload completes, and have some server-side code copy or rename the image to the required path: https://cloudinary.com/documentation/upload_images#notifications_and_asynchronous_transformations`
Please take a look and see if any of those options would work for you; I'd be happy to explain in more detail if you can let me know some more about your current workflow and which option above most closely matches it.
Regards,
Stephen
0
Post is closed for comments.
Comments
1 comment