Everest
Everest

Reputation: 607

Upload a file to server via URL using nodejs and expressjs

I would like to be able to POST to my express app with a file URL and download that file to my own server.

For example, I display a list of images that are taken from a third-party and when the user clicks download, it will send a post request to this node app with the file URL (http://example.com/image.jpg) and download it to my server.

How would I do this? I apologize as I'm very new to node.

Upvotes: 3

Views: 7215

Answers (2)

Michael
Michael

Reputation: 6899

Use the Formidable module to handle file uploads. It handles many tasks and features related to file upload.

npm install formidable@latest

Upvotes: 1

FabioCosta
FabioCosta

Reputation: 2749

To download a remote file from node you could save what you would get from a http GET request to that file:

    var http = require('http');
    var fs = require('fs');//Handle files
    var fileToDownload=req.body.fileToDownload;
    var file = fs.createWriteStream("externalImage.jpg");
    var request = http.get(fileToDownload, function(response) {
      response.pipe(file);
    });

Upvotes: 3

Related Questions