javascript - 将文件写入与工作目录不同的目录

标签 javascript node.js fs

我的项目结构是这样的

enter image description here

现在,我需要将 asset.js 中的文件写入 pdf 文件夹中的文件。

这就是我正在尝试的

var qrImgPath =   '/lib/pdf/' +eod+'.png';
                    fs.writeFile(qrImgPath,body,'binary',function(err){
                        return next();
                    });

但我收到以下错误

{ handle: 2,
  type: 'error',
  className: 'Error',
  constructorFunction: { ref: 5 },
  protoObject: { ref: 6 },
  prototypeObject: { ref: 1 },
  properties: 
   [ { name: 'stack',
       attributes: 2,
       propertyType: 3,
       ref: 1 },
     { name: 'arguments',
       attributes: 2,
       propertyType: 1,
       ref: 1 },
     { name: 'type',
       attributes: 2,
       propertyType: 1,
       ref: 1 },
     { name: 'message',
       attributes: 2,
       propertyType: 1,
       ref: 7 },
     { name: 'errno',
       propertyType: 1,
       ref: 8 },
     { name: 'code',
       propertyType: 1,
       ref: 9 },
     { name: 'path',
       propertyType: 1,
       ref: 10 } ],
  text: 'Error: ENOENT, open \'/lib/pdf/b0551796a741aa885e641dbd895a233f.png\'' }

.

我怎样才能实现这个目标?

最佳答案

您可以使用__dirname。 (https://nodejs.org/docs/latest/api/globals.html#globals_dirname)

path.join(__dirname, "../../lib/pdf" + eod + "png");

关于javascript - 将文件写入与工作目录不同的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738371/

相关文章:

javascript - 如何在给定日期的情况下返回正确的星期几 - 年月日

javascript - 按可以未定义的 'order' 字段对数组进行排序

mysql - 使用 handle 访问 SQL 对象以在屏幕上呈现

node.js - 如何自动化API获取数据请求?使用网络套接字时

node.js - Node.js 中的 fs.ReadStream 和 fs.createReadStream 有区别吗?

javascript - Vue.js 模板连接 Prop

javascript - 更新 Windows Azure 移动服务中的查询 javascript/html

javascript - 按名称、日期和层次结构对列表进行排序

node.js - Node 的 fs.symlink() 认为链接已经存在;绝对不会

javascript - stats.isFile() 无法识别路径中的文件