I have an application that displays a photo gallery. Under each photo there is a button which, when clicked, is supposed to save the photo on the user's computer. Unfortunately, the photos are saved as an HTML document when downloaded. That is why they cannot be opened after downloading. The photos are stored in a static uploads folder attached to the project using nodejs
. How do I download a photo so that it is saved in the correct format?
What I have already tried is using the download
attribute by placing the photo under the a
tag as here:
<a href={file.path} download>download</a>` or `<a href={file.path} download={file.name}>download</a>
.
I also tried using the file-saver
module, but with no positive result. With this solution, the image is also saved as an HTML documen:
const Gallery = () => {
//
const downloadImage = (file) => {
saveAs(
`http://localhost:3000${file.path}`,
file.name + '.' + file.type
)
}
//
return (
<button onClick={downloadImage(currentFile)} type='button'> download </button>
)
}
In react-router-dom, I don't have the route set so that I can preview the photo directly after clicking on the photo.