Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.9k views
in Technique[技术] by (71.8m points)

node.js - How to change directory. Multer/express.js

I have two form like this

<form action="/createFolder" method="POST" enctype="multipart/form-data">
    <input type="text" name="folder" id="year" placeholder="enter year">
    <input type="text" name="name" id="student" placeholder="enter your name">
    <input type="submit" value="send">
</form>

<form action="/upload" method="post" enctype="multipart/form-data">
    <label>File</label><br>
    <input type="file" name="filedata" />
    <input type="file" name="filedata" />
    <input type="file" name="filedata" /><br><br>
    <input type="submit" value="Send" />
  </form>

in the first one user enter data and my node.js script create two folders like this "year/name"

app.post('/createFolder', upload.none(), (req, res, next) => {
 const body = req.body;
 if (fs.existsSync(body.folder)) {
    console.log('Folder exist');
 } else {
    fs.mkdirSync(body.folder);
 }
 fs.mkdirSync(body.folder + '/' + body.name);
 updateStorageConfig(body.folder, body.name);
 res.redirect("/");

In the updateStorageConfig function i try to set multer.diskStorage() and then create app.post for my second form like this

function updateStorageConfig(folder, name) {
storageConfig = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, folder + '/' + name);
    },
    filename: (req, file, cb) => {
        cb(null, file.originalname)
    }
});

upd = (multer({storage: storageConfig}).array("filedata", 3));
app.post("/upload", upd, function (req, res, next) {
    let filedata = req.files;
    if(!filedata)
        res.send("Error");
    else {
        res.end();
    }
});

If i create the fist folders its work ok, but when i create another folders, files from second form downloads in old direction.

How change the direction??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...