Upload and Retrieve Image in Node.js? - javascript

I'm new to nodejs. I'm trying to upload an image to the server and store that image path in the localhost's PostgresSQL database as like this (myipaddress:3000/uploads/images/12345.png).
I Learned to upload an image to the server as specified in this link
But I would like to load that image url separately in mobile device or in image tag as follows <img src="myipaddress:3000/uploads/images/12345.png">
Thanks in advance and Can any one please provide suggestions to access an uploaded image with its url in my express application

Follow this (http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html) to upload an image to server in nodejs.
Once you followed. It will return response like this
File uploaded to: ./public/images/85d15c2f7e812a03faa44bdef0ce41f5.png - 278070 bytes
The response shows that your image is stored in the public/images directory
Image Name : 85d15c2f7e812a03faa44bdef0ce41f5.png
Image Path : ./public/images/85d15c2f7e812a03faa44bdef0ce41f5.png
You can load the image with the server ip by removing the public from the URL. Because we can directly access the content from the public directory without specifying public.
So the following link is enough to load the image from the URL.
your-local-ip:port/images/85d15c2f7e812a03faa44bdef0ce41f5.png
eg:
http://localhost:3000/images/85d15c2f7e812a03faa44bdef0ce41f5.png

Related

Display image on Html page

I'm trying to display a static image located in the same folder as my Html file but it seems I can't get the right path for it to display correctly. The application I'm developing is an atlassian plugin that also includes a java backend to get Data from the Database and I'm displaying it on the frontend using HTML and javascript, the whole application runs on a webserver as a Plugin. Both the image and the Html file are located in here: D:\clone4\project\src\main\resources\templates\scheduleraction
The URL path for the web application is:
https://staging.com/jira/secure/SchedulerAction!default.jspa
I tried many ways and this is the last one :
<img src="/SchedulerAction!default.jspa/piechart.jpg" alt="pie-chart">
I need to add the correct path in the "src" so the client can retrieve the image from my Files on the webserver. I would love any hint or help!
you should use exact path like ./Image.png. And avoid using Images out of the static HTML file located directory
Try to use a service to upload that image and use the provided url.
Such as https://imgur.com/. After upload it, use into the src the correct url with the filetype extension.

Saving user uploaded image to folder and/or server

I have an app, that allows users to upload an image, crop it and with other data save it all as html file to be used as a footer for emails. The image is given to them as base64.
Howver turns out this is not supported by Outlook, since it doesnt accept b64 data as img source.
So my idea was to save the cropped image to a file, let's say /public/avatars/avatar.png and link it's directory as a source. However I'm having trouble finding a way how to save images to to a file using JS. My allowed stack is JS and React, no node.js.
How would I do that? I can have the file as either b64 ot canvas element, so i'm flexible here, as long as it's saved to file.
I'm open to other solutions too.
You can't save a file with client language only. You have to save it to a server, own server or external server or a service like AWS.
The best solution without server-side (strangly) is to use a API for save image and get link from this API. Then you can use this link to Outlook.
You can use https://aws.amazon.com/fr/cloudfront/ free for one year with 50Go and 2 millon request monthly.
If you do not exceed 300,000 images per year you can use this service : https://cloudinary.com/pricing
You can also use https://www.deviantart.com/developers/ but that's not really the point of service.
Weird solution :
Be careful, the login and password of your FTP user will be available in the source of your code. Minimum rights must be administered.
You can use this script for talk to FTP server from JS (not tested but seems worked) : http://www.petertorpey.com/files/ae/scripts/FTPConnection.jsx
You can try something like that :
var ftp = new FtpConnection("ftp://URL") ;
ftp.login("username", "password");
ftp.cd("FOLDER") // For move to folder
ftp.put(file,"FILE.PNG") ; // File must be a File JS Object
ftp.close() ;

image not found / can't get /uploads/image nodejs REST API

I am trying to send an image to client in nodejs/expressjs REST API. What I am trying to do is saving that image url in mongo db database. When user make a get request, I send all the necessary data alongside the image which is basically avatar. Now on the client side I am not able to receive that image. I am keep getting below responses.
in console tab of chrome dev tools I am getting below.
When I see in the network tab I get below response which also has the link to avatar. Everything is perfect here.
Now when I check my uploads folder I do have the image. Check below:
After all this I am still getting these errors and still not sure where exactly am I making mistake. One thing is that maybe the client is trying to make a request to /uploads/image end point which I do not want. I simply want that image to be displayed using the url I am getting on response.
As we do not see the configuration of static files in express, this might be the problem.
To serve static files in your /uploads folder you need to add this line
app.use('/uploads', express.static('uploads'));
The 'uploads' directory will not be accessible from the frontend.
In node, move the uploaded file in to a subdirectory of the public directory of the node app, and set a relative path from there ans the image path.

Get server URL from file input in Javascript

I need to manage some files who are uploaded to my server, but I need to get the URL of the selected file. I know you can't do this with javascript with files outside the server, but I wanna know if there's a way to get the URL from files in the server. For example I got this image from my file input with a selected file (LTAIPBCS75FI.xls) who is hosted in "xls/ITAI2016/Fraccion01/" in my home directory. I get this URL from php:
URL IN FILE INPUT
I need then to get the full directory when I select a file in my server.

Display uploaded image

After some research What is the best place for storing uploaded images, SQL database or disk file system?, I decided to store the uploaded pictures on file system e.g. C:/images/image.jpg, and store this path into a database.
Now when I want to display the picture by setting attribute src="C:/images/image.jpg" I get error in console -
Not allowed to load local resource:
What is the proper way to display images stored in filesystem ?
I found this site https://howtoprogramwithjava.com/how-to-display-images-stored-on-a-server/ which solved my issue. I had to setup a context path for the webapp.

Categories