1

I have a frontend entry form which included a file field to upload files to the entry. All works fine. I even have a delete function to remove individual uploaded files from the entry.

The problem is if a user wants to attach a file they have already uploaded. Uploading the file results in the same file uploaded but with a different filename (a timestamp added to the end).

This is going to result in unused files being left on the server and a nightmare version control going on - people left wondering what version is correct, which is being used, is it safe to delete the old ones etc.

So ideally what is needed is the user can browse the assets uploaded on the front end and ideally be able to upload within that.

Is there a facility to do this?

Thanks

| improve this question | | | | |
3

For Craft 3, there is UploadIt, a front end asset uploader.

| improve this answer | | | | |
2

Craft doesn't store information about the uploader for assets, so, if you want to make sure each user only sees their own respective assets, you could use dynamic subfolder paths and account for that when listing the available assets for that user. For example, you could use a dynamic path user-uploads/{{currentUser.id}}. Later, when querying for the assets, you could do

{% set folder = craft.app.assets.findFolder({path: 'user-uploads/' ~ currentUser.id}) %}
{% set assets = craft.assets.folderId(folder.id).all() %}

Another approach to take when separating downloads into folders by users is making use of temporary upload folders, which are separate for each user. You can do that by calling {% set folder = craft.app.assets.getCurrentUserTemporaryUploadFolder() %}, which will set the folder variable to an instance of craft\models\VolumeFolder.

You can use that as a starting point to build your own simple asset browser and then for saving asset relation data to any relations field (Assets, Entries, Users..) you just save the entry and submit a list of ids that you want.

| improve this answer | | | | |
  • "not sure if you just had a "whoosh" moment or the question is about something else" -> the backend allows you to browse and therefore re-use or delete uploaded assets. The front end allows no such convenience. If you upload using a file upload field in a form, it creates a new file, even if you upload one with the same filename. So I was asking if there was a means to do so. I really dont think everyone should re-invent and develop their own assets browser for the front end so wanted to know if one exists. – Laurence Cope Dec 17 '18 at 13:29
  • Unfortunately, I don't think anyone has built a front-end asset management component. But I've just updated my answer to show you how your users can see a list of assets they've already uploaded. – Andris Sevcenko Dec 17 '18 at 14:07
  • Ok thanks. I guess a simple asset management system would therefore need to be built on every site that has a simple file upload field if you want to make sure duplicate files are not uploaded and give users the option to delete files from the server. – Laurence Cope Dec 18 '18 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.