繁体   English   中英

使用nodeJS将文件添加到同一文件夹

[英]Adding files to same folder with nodeJS

我想将mp3, lrc, wav, txt到服务器,并且文件夹名称将为title并且上述所有扩展名均具有指定的名称。 如代码中所示,mp3将为"vocal.mp3"

addSong(event) {

    jQuery('#addItemSave').text("Please wait!");

    this._service.addSong(this.authorText, this.titleText, this.durationText, this.genre).subscribe(
        res => {
            this.data.push(res.data[0]);
            this.addSongFilesToServer();
    });
}

private addSongFilesToServer() {
    this._service.addSongFilesToServer(this.titleText,
            this.mp3file, this.lrcfile, this.pitchfile, this.thumbfile).subscribe(
        res => {
            console.log("Done! Now hide loading icon or what ever...");
            jQuery('#addItemSave').text("Save");
            jQuery('#addNewSongDialog').modal('hide');
            jQuery('#addNewSongDialog').find("input,textarea,select")
                    .val('')
                    .end()
                .find("input[type=checkbox], input[type=radio]")
                    .prop("checked", "")
                    .end();
    });
}

addSongFilesToServer(title, mp3, lrc, txt, thumb) {
    let url = this._config.ServerWithApiUrl + "uploadFiles";
    let body : FormData = new FormData();
    body.append('mp3', mp3, "vocal.mp3");
    body.append('txt', txt, "pitches.txt");
    body.append('lrc', lrc, "lyric.lrc");
    body.append('thumb', thumb, "thumbnail.png");
    body.append('title', title);
    this._config.headers.delete('Content-Type');

    return this._http.post(url, body, { headers: this._config.headers })
        .map(res => res.json()).catch(function(err){
            throw err;
    });
}

当在页面上按下按钮并传递所有需要传递的文件时,将addSong(event) 唯一的问题是mp3, lrc, wav, txt都带有它们的不同文件夹,例如vocal.mp3

只是为了说明一下,这就是我目前所得到的:

├───songs
│   ├───vocal
│   │    vocal.mp3
│   ├───pitches
│   │    pitches.txt
...

这就是我需要的:

├───songs
│   ├───title
│   │    vocal.mp3
│   │    lyric.lrc
│   │    pitches.txt
...

和服务器端:

Router.post('/uploadFiles', (req, res) => {

    var title = req.body.title || "title";

    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
            var dir = "../songs/" + file.originalname.split('.')[0];
            if (!fs.existsSync(dir)){
                fs.mkdirSync(dir);
            }
            cb(null, dir);
        },
        filename: function (req, file, cb) {
            cb(null, file.originalname);
        }
    });

    var upload = multer({ storage : storage}).any();
    upload(req,res,function(err){            
        if(err){            
            return res.status(500).send({ 
                code: 500, message: 'could not upload file: ' + err, error: err });
        }else {
            return res.status(200).send({ 
                code: 200, message: 'All files uploaded!', err: ""});
        }                     
    });       
});

您可以在示例中看到实际上是通过这种方式编码的

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        var dir = "../songs/" + file.originalname.split('.')[0];
        if (!fs.existsSync(dir)){
            fs.mkdirSync(dir);
        }
        cb(null, dir);
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});

file.originalname.split('.')[0]实际上是分割扩展名并使用文件名。

因此它将是var dir = "../songs/" + title; 干净利落。

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        ////////////////////////////
        var dir = "../songs/" + title;
        ///////////////////////
        if (!fs.existsSync(dir)){
            fs.mkdirSync(dir);
        }
        cb(null, dir);
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});

另外, body-parser不能处理多部分主体,这就是FormData提交的形式。 因此, req.body.title将不起作用。

检查以供参考: 如何处理Express 4中的FormData
您很可能需要用其他方式发送title

暂无
暂无

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

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