File upload issue

Using NodeJS and a local server for development, I’m trying to upload a pdf document to a directory created in “/public” folder.
In the settings I set up links relative to the Site Root:


BTW: Users upload directory is wrong - there should not be public prefix as in assets but it is added automatically after selection of directory.

In my action I have to put the full path and not the path relative to the site root:


Otherwise the upload is not working.

After the upload I write the path into a database:


When I try to write an url it is always a NULL value.


When I try to open the file using the stored path in the database the file cannot be found (404). It can be found using the path without the “public” directory prefix on the beginning of the stored path.

Is is a bug or I’m missing something?

Dominik

So you just need the /public removed from the database insert? or removed from the link once its posted? Either way you should be able to use a formatter to remove the ‘/public’ part of the URL.

I just have to remove the /public from the database insert. Of course it is doable with the formatter but this should not be stored in the database in such a way.
Relative to the site root defines how path shoud be returned - i think this is the issue.

The other thing is that url from the file upload action returns NULL.

Dominik.

Are you uploading one file, or are you uploading multiple with a repeat?

It’s due to the way that node servers work and the fact that Wappler has to cater for multiple server technologies

Your upload path is correct /public/faktury
This is required as the path to the directory

HOWEVER node treats the /public folder as the site route so the path to the file is not the same
if the file is called “myimage.jpg” the the path to the file with reference to the browser is simply:
/faktury/myimage.jpg

Best practice is to only store the filename in your database

Then when displaying the image in app connect append the path to the image
So your image link will be /faktury/{{imagename}}

like this:
image

Obviously substitute “imagename” with the name of your database field

Sorry, missed you are using a pdf, not an image, technique is identical

Brian explains the reasons behind this very well. I handle it a bit differently though (not better, just different).

In my database I have a typical varchar to store the entire path to the file. I use this when I need to delete the actual file, or load it for processing on the server etc.

I then add a second generated column that is simply a pointer to the original file path, but removes /public/ from the string.

When doing a query to retrieve the path needed from an img tag, or background, I use the generated column instead of the physical file path.

The main reason for this approach is that I only have to set this up once and never have to think about the path again. Saves me from making typos.

OK. I think I get it, but still do not know why assets are simply /assets and custom directory adds “public/” in front of the directory.

It seems however I should manually remove /public each time the path is written to the databse.

Thank you very much for the explanation !

I like your idea but as I mentioned before the url returns empty string on upload.
Thank you for sharing :slight_smile:

If use direct File Upload Action the value on database should be {{upload.url}} and not path.
Now if use Resize, Load Image and Save process I do this to solved:

File Upload
name: upload_sample
Upload Fields: $_POST.file_upload
Path: /public/images/sample

Load Image
Name: sample_image
Image Path: upload_sample.path

Resize
Image: sample_image
Custom Width and Height

Save Image
Name: newImage_sample
Image: sample_image
Path: /public/images/sample/resize

Database
Value: newImage_sample.replace(’/public/’, ‘/’)

I use the formatter replace to search ‘/public/’ and replace with ‘/’.

I really preffer to store the path and just like @mebeingken say, I never bother to type custom paths in html page, one-time process for me.

I don’t know if this is the best approach but help me to solve and save a lot of time.

1 Like

Use whatever best suits your needs
I tend to use the custom paths in html and image name only in DB as i tend to store multiple copies of images at different sizes into separate folders so i can use optimised images where appropriate.
For example i will have

/foldername/raw (image as uploaded, unchanged)
/foldername/large (perhaps 1280 or 1024 wide)
/foldername/mid (perhaps around 640 wide)
/ffoldername/raw (perhaps around 160 wide

so depending on which image i want to display, my path will be different for example
a main image may be /foldername/large/myimage,jpg
but a list view may only need a thumbnail
/foldername/thumb/myimage,jpg
If i need to store full paths then i would have to store the full paths 4 times

Use whatever best suits your specific needs, it’s down to preference and use case