Sails.js如何在上传后获取图像名称

问题描述 投票:0回答:1

我希望得到图像的名称,并在上传后将其插入MySQL。结果我得到了商店图片路径。像MySQL上的E:\ work \ assets \ pic_items \ 98046f37-ac7a-42cc-996.png我上传后只需要图像名称。

我怎样才能做到这一点?

这是我的控制器

update: function (req, res){
    var id = req.param('id');
    var category_id = req.param('category_id'); 
    var title = req.param('title');
    var description = req.param('description');
    var width = req.param('width');
    var height = req.param('height');
    var price = req.param('price');
    var picture_path = req.param('picture_path');


    console.log(id);

    req.file('picture_path').upload({dirname: "../../assets/pic_items"},function (err, uploadedFiles){
        if (err) {
            return res.send(err);
        }   
        console.log(uploadedFiles);
        if (uploadedFiles.length === 0){
            console.log(uploadedFiles);
            return res.serverError('no file was uploaded'); 

        }
        picture_path = uploadedFiles[0].filename;
            picture_path = uploadedFiles[0].fd.replace(/^.[\\\/]/, '');


    Item.update({id:id},{category_id:parseInt(category_id),title:title, description:description, width:width, height:height, price:parseInt(price), picture_path:picture_path}).exec(function(err){
        if(err){
            console.log(err);
            return res.send('life is suck');
        }
            res.redirect('/item');
        });
    });
},
javascript mysql sails.js
1个回答
0
投票
picture_path = uploadedFiles[0].filename;
            picture_path = uploadedFiles[0].fd.replace(/^.[\\\/]/, '');

我相信它是在uploadedFiles[0].filename。你为什么用uploadedFiles[0].fd.replace(/^.[\\\/]/, '')压倒它?

© www.soinside.com 2019 - 2024. All rights reserved.