Next.js Discord

Discord Forum

Downloading an Image

Answered
Satin Angora posted this in #help-forum
Open in Discord
Satin AngoraOP
I'm currently building a simple gallery, files are uploaded to a backend server thats serving the images using nginx on a different subdomain than my project.
Like images are uploaded to files.example.com and my next application is running at share.example.com.

Now I want to add a "download" button next to each image or file that will download the image and save to disk but I cant find a way to do that. All methods I've found on google are just opening the image in a new or the same tab instead of starting a download or opening the window to choose a location to save the file.
Files like zip, pdf, rar etc are working fine but images / gif or videos are just opened in a new tab.
<a href={data.url} download={data.title + data.filetype}>Download Image</a>


I've also tried it with fetching the content / objecturl and creating the anchor manually + using anchor.click(), same result as above. Also tried the file-saver package that pops up in every second google search result.

Any ideas how to solve my problem?
Answered by Ray
I just tried with <a href={image.url} download>Download Image</a> and it works
View full answer

4 Replies

I just tried with <a href={image.url} download>Download Image</a> and it works
Answer
Satin AngoraOP
hm, thats strange. I've tested it with firefox, chrome and edge and all browsers just open the image in a new tab. No errors in console or anything.
can you show me the url that not work?
Satin AngoraOP
nvm its working when deployed, just on localhost its behaving this way for some reason.