0

I have a file in D: Drive of my local system. I need to download the file into the E: Drive. How to do this using node.js and http request? I am a beginner in node.js. Please give me valuable suggestions.

Note: The file may be in any type.

3
  • 2
    Why do you need http? all you need is to move your file from one drive to another. Commented May 7, 2014 at 13:04
  • but the requirement is to use http request. Commented May 7, 2014 at 13:09
  • Is it a text file or a binary file? Commented May 7, 2014 at 13:29

2 Answers 2

3

Here is an example:

// upload.js
var fs = require('fs')

var newPath = "E:\\newPath";
var oldPath = "D:\\oldPath";

exports.uploadFile = function (req, res) {
    fs.readFile(oldPath, function(err, data) {
        fs.writeFile(newPath, data, function(err) {
            fs.unlink(oldPath, function(){
                if(err) throw err;
                res.send("File uploaded to: " + newPath);
            });
        });
    });
};

// app.js
var express = require('express'), // fetch express js library
upload = require('./upload'); // fetch upload.js you have just written

var app = express();
app.get('/upload', upload.uploadFile); 

app.listen(3000);

Basically there are two parts, one doing the copying from one drive to another, and the other one is for triggering. Once you run you app.js and make a GET request to localhost:3000/upload it will copy the file from newPath to the oldPath. For further information have a look to expressjs and fs.

Sign up to request clarification or add additional context in comments.

3 Comments

while running, i am getting error as "Error: EPERM, open 'E:\'"
hi.. its working fine. But when downloading the file, the oldpath file gets removed. I want the file in old path also not to get deleted. Is it possible?
I commented fs.unlink line. now it gives the desired result. thank you sp much.
2

Assuming it's a text file, you would have to write two node.js server.

The first would answer (all/specific, your choice) http get with the content of the file, the other would make a get and download the file.

server.js: Will work only for text file

var http = require('http'),
    fs = require('fs'),
    server = http.createServer(function (req, res){
        res.writeHead(200, {'content-type': 'text/text'});
        fs.readFile('E:/path/to/file.txt', function (data) {
            res.write('' + data);
            res.end();
        });
}).listen(8080);

client.js

var http = require('http'),
    fs = require('fs'),
    file = fs.createWriteStream('D:/path/to/new.txt', {flags: 'w'});
http.get('http://localhost:8080', function (res) {
    res.pipe(file, {end: 'false'});
    res.on('end', function() {
        file.end();
    });
});

EDIT:

The only advantage versus anvarik's solution is that I don t use express...

4 Comments

yeah but your solution is only for text :) mine is for all kinds of data
@anvarik: Yup, that s why I say it s the only advantage ; )
Hi, how to download all type of files?
and the above code not downloads the file and it returns as null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.