Best way to download remote images to local filesystem in Node.js?

Hi Wapplers,

I need to download images from various external URLs to my local server storage within a Node.js project.

My Requirements:

  1. Download: Fetch an image from a remote URL.

  2. Save & Rename: Save the file to a specific local folder (e.g., /public/assets/images/) and give it a custom name.

  3. Reliability: Some external servers block simple requests.

My Questions:

  • Is the "File Download" component the best choice for this, or should I use an "API Action" and save the response as a file?

  • How can I pass a User-Agent header to the request to avoid being blocked (403 errors) by remote servers?

  • Are there specific settings in Node.js to ensure the download doesn't hang if a remote server is slow?

Setup:

  • Wappler: Latest

  • Server: Node.js

Thanks for any tips or best practices!

Sounds like you'll need a smart file retrieve utility since you'll come across various server responses (or obstacles). The server connect's File Download component, in my experience, is for allowing a user to download a file to the browser from the server, not to retrieve a remote file. I don't believe that the File Download component will serve your needs. API Action allows you to interact with remote API's, so this will help you only when the remote image can be retrieved by a remote API. If the image is just sitting in a public folder somewhere, the API Action won't be any help.

If the number of images to be retrieved and the rate of retrieval are both modest/reasonable, a front-end solution may work best for you. A user could enter in a URL, or a list of URL's, and Javascript could easily retrieve the image files, include error handling, sending HTTP headers, etc., then POST them to your own API on your local server for saving.

Take a look to this extension:

1 Like