Save uploaded file in table

Hello,

I am working in codeless UI builder and i have used the file uploader button to upload files to a location. This is working.
Now i want to store that url into the table so the user can retrieve the file.
What is the correct way to do that? I am using the Save Object in Backendless block to store the data from a form, and i want the add the url to the table.

I have created the table with a column with FILE REFERENCE type.

I am now using this url:
https://domain/api/files/folder/file.pdf

This is working, but i am not sure if that is the correct way of doing this.
Now i want to restrict the access to this file by setting the permissions in the Files but this does not work. When i give the permissions to one specific user, then this user cannot see the file.
Only when i set the ServerCodeUser READ setting to OK then i see the file, but then any user can access it.

I too need this exact feature

To store a “reference” to the file in a column with the FILE REFERENCE type, you need to save the file URL. Once you upload the file, you get the URL back of the uploaded file. Use that URL in the property of the object you save/update.

Could you please show how you restrict access to the file?

Hi @mark-piller,

I know have this in the filename logic:

I changed in this example the real domain with domain.

How do i get the fileURL in codeless?

Are you asking how to get file URL as a result of file upload?

@mark-piller Yes,

When i use this file in page data as in the example, when the user clicks on the link then he gets this url: https://eu.backendlessappcontent.com/APPID/files/facturen/

But i think i need to have the https://domain/api/files/facturen/… in order for the restricting to work correctly

image

Here’s the file upload component, go into the Logic for it:

in there you will need on Upload Success:

does it help?

Hi Mark,

How should i have the URL?

Like this?
url: https://eu.backendlessappcontent.com/APPID/files/facturen/…

Or like this?
https://domain/api/files/facturen/…

both URLs point to the same file. By default the URL will be eu.backendlessappcontent.com. This is because the UI Builder app uses the REST API Key:

If you change the published app API key to use your subdomain/domain, then the URL of the uploaded files will reference the selected domain.

Thanks @mark-piller,

So for restricting the visibility of users it wouldnt make a difference?

For restricting the access i have set all roles permissions to X.

Then for 1 user i have added the permissions:
image

image

The URL you get and permissions are not related. URL is how a file is referenced. Permissions control what roles can or cannot get the file.

If you added permissions for a specific user, the file can be downloaded only by that user ONLY IF there if the request to get that file has the user-token HTTP header. If the http header is not passed, the request will be handled as if from a non-authenticated user.

OK @mark-piller thats what i am missing.

How do i pass the user-token http header with codeless logic?

This will not work because the token is not part of the URL. Could you please show the logic you use to fetch the file?

Hi @mark-piller,

This is the File Name Logic in the File Uploader Button:

I then save the object in backendless property ‘file’ in page data which is saving the object in the FILE REFERENCE column in the table.

I then use link to fetch the file which relates to the FILE REFERENCE column in the table, column name = file.
image

image

@mark-piller,
Could it work with a HTTP/S block?
image

Sorry for the long response.

No, it doesn’t.

HTTP block invokes an API call, but this handler should return a string.

What about your situation in general, we should investigate a situation with cookie a little deeper, and now we can’t offer you any workaround.

1 Like

@Lorio Could you confirm whether you enable Cookie-based authentication?

I have now

If now it’s enabled, you can use a link to download like <domain-name>.backendless.app/api/files/<filename> and that should works.

Hi Dima,

I have a custom domain, so will it then be custom domain/api/files/ ?