最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Write file to a subdirectory node.js - Stack Overflow

programmeradmin1浏览0评论

I am working with node and express and i have a directory structure like

public
   img
   css
   js
server.js

in server.js i have the following code for writing to a png file

fs.writeFile('testfile.png', imageData, function(err){

        res.send("file successfully created");

    });

this creates the file in the root i.e. with the following directory structure

public
   img
   css
   js
server.js
testfile.png

How do I create testfile.pnginside the img directory? simply using img/testfile.png, /img/testfile.png or /public/img/testfile.png does not work-- no file is created.

Help appreciated!

I am working with node and express and i have a directory structure like

public
   img
   css
   js
server.js

in server.js i have the following code for writing to a png file

fs.writeFile('testfile.png', imageData, function(err){

        res.send("file successfully created");

    });

this creates the file in the root i.e. with the following directory structure

public
   img
   css
   js
server.js
testfile.png

How do I create testfile.pnginside the img directory? simply using img/testfile.png, /img/testfile.png or /public/img/testfile.png does not work-- no file is created.

Help appreciated!

Share Improve this question asked Feb 5, 2013 at 4:37 algorithmicCoderalgorithmicCoder 6,79521 gold badges75 silver badges118 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can do this

'./public/img/testfile.png'

Or, you can give relative path like this

__dirname + '/public/img/testfile.png'

Also check for the permission of the directory img. May be the write permission is not given.

发布评论

评论列表(0)

  1. 暂无评论