Question

I am downloading a file from one drive to another drive in my local system. This is the code which I have used:

upload.js:
   var fs = require('fs')
   var newPath = "E:\\Thevan";
   var oldPath = "E:\\Thevan\\Docs\\something.mp4";

   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);

When I run the above code.. It gives error as "Error: EISDIR, open 'E:\Thevan'". How to give the path or anything went wrong in the above code?

Was it helpful?

Solution

The best way to work with paths - use path module (API):

Or with path module:

var path = require('path');
path.join('E:','Thevan','Some File.mp4');

It also fix more issues if you switch to Linux.

But you have an other problem - you pass DIRNAME instead of FILENAME to writeFile function. I think you mean:

var newPath = "E:\\Thevan\\something.mp4";
var oldPath = "E:\\Thevan\\Docs\\something.mp4";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top