[英]Write a file to a directory which is different from working directory
我的項目結構是這樣的
現在,我需要將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");
您可以使用__dirname全局變量,該變量返回當前正在執行的腳本所在的目錄的名稱。
因此,您的代碼應類似於:
var qrImgPath = path.join(__dirname, '../lib/pdf/' +eod+'.png');
fs.writeFile(qrImgPath,body, 'binary',function(err){
return next();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.