admin管理员组

文章数量:1279146

I tried to uplaod file and move to new directory already exists. follow Writing files in Node.js but I got the error: Error: EISDIR, open '/Users/name/Sites/project/app/assets/images/UploadTemporary/' at Error (native)

and I found Using Node.js I get, "Error: EISDIR, read" and Node.js Error: EISDIR, open Error similar error message, my UploadTemporary folder already exists do I mess something wrong? I don't get it, if its not a directory what else can be?

var multipart = require('connect-multiparty');
var fs = require('fs');
var path = require('path');
var appDir = path.dirname(require.main.filename);

...
var sourceFile = req.files.file[0].path;
var destinationFile = appDir + '/assets/images/UploadTemporary/';

var source = fs.createReadStream(sourceFile);
var destination = fs.createWriteStream(destinationFile);

source.pipe(destination);
source.on('end', function () {
  fs.unlinkSync(sourceFile);
});

I tried to uplaod file and move to new directory already exists. follow Writing files in Node.js but I got the error: Error: EISDIR, open '/Users/name/Sites/project/app/assets/images/UploadTemporary/' at Error (native)

and I found Using Node.js I get, "Error: EISDIR, read" and Node.js Error: EISDIR, open Error similar error message, my UploadTemporary folder already exists do I mess something wrong? I don't get it, if its not a directory what else can be?

var multipart = require('connect-multiparty');
var fs = require('fs');
var path = require('path');
var appDir = path.dirname(require.main.filename);

...
var sourceFile = req.files.file[0].path;
var destinationFile = appDir + '/assets/images/UploadTemporary/';

var source = fs.createReadStream(sourceFile);
var destination = fs.createWriteStream(destinationFile);

source.pipe(destination);
source.on('end', function () {
  fs.unlinkSync(sourceFile);
});
Share Improve this question edited May 23, 2017 at 12:22 CommunityBot 11 silver badge asked Aug 11, 2015 at 7:11 user1775888user1775888 3,31314 gold badges49 silver badges67 bronze badges 3
  • 1 You need to give the actual filename in the destinationFile – thefourtheye Commented Aug 11, 2015 at 7:13
  • you mean something like this var destinationFile = appDir+'/assets/images/UploadTemporary/'+newfilename – user1775888 Commented Aug 11, 2015 at 7:14
  • 1 Exactly. I think you are thinking that giving just the directory name will create a file with the source file name, right? – thefourtheye Commented Aug 11, 2015 at 7:15
Add a ment  | 

1 Answer 1

Reset to default 9

When you are writing a file to a specific directory, you need to give the actual destination file name as well. Unlike cp mand, the destination filename will not be inferred by fs module.

In your case, you are trying to write to a directory, instead of a file. That is why you are getting EISDIR error. To fix this, as you mentioned in the ments,

var destinationFile = appDir + '/assets/images/UploadTemporary/' + newfilename;

include the file name as well.

本文标签: javascriptnodejs Error EISDIRopenStack Overflow