繁体   English   中英

fs.createWriteStream,没有此类文件或目录,请打开Nodejs

[英]fs.createWriteStream, no such file or directory, open Nodejs

我想保存从另一台服务器上获取的文件,但是问题是当我调用createWriteStream时出现错误:

没有此类文件或目录,请打开E:\\ pathtoproject \\ myproject \\ public \\ profile_14454.jpg

这是我在E:\\pathtoproject\\myproject\\modules\\dowload.js

request.head(infos.profile_pic, function(err, res, body) {
  const completeFileName = '../public/profile_14454.' + res.headers['content-type'].split('/')[1];
  var imageStream = fs.createWriteStream(completeFileName);
  imageStream.on('open', function(fd) {
    console.log("File open");
    request(infos.profile_pic).pipe(imageStream).on('close', function(body) {
      consoleLog('Profile pic saved');
      console.log('This is the content of body');
      console.log(body);
      connection.query('UPDATE user set photo=? where id=?', [completeFileName, lastID], function(err, result, fields) {
        if (err) {
          consoleLog('Error while update the profile pic');
        }
      });
    })
  });
});

当我删除目录../public/并仅保留文件profile_14454.' + res.headers['content-type'].split('/')[1]的名称时profile_14454.' + res.headers['content-type'].split('/')[1] profile_14454.' + res.headers['content-type'].split('/')[1] ,它可以工作,但是文件保存在项目的根目录中( E:\\pathtoproject\\myproject\\ )。

我在做什么错了? 如何将文件保存在公共目录下?

我正在使用nodeJS 8.9.4

我尝试使用我的小代码。

var fs = require("fs");
var data = 'Simply Easy Learning';

// Create a writable stream
var writerStream = fs.createWriteStream('./airo/output.txt');

// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');

// Mark the end of file
writerStream.end();

// Handle stream events --> finish, and error
writerStream.on('finish', function() {
    console.log("Write completed.");
});

writerStream.on('error', function(err){
   console.log(err.stack);
});

console.log("Program Ended");

我的代码在此路径E:\\syed ayesha\\nodejs\\nodejs现在我要将文件存储在此路径中的airo文件夹中。 所以我用一个点来存储。 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM