亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在強大的上傳中使用 compress_image 時“文件不存在”

在強大的上傳中使用 compress_image 時“文件不存在”

拉莫斯之舞 2022-10-13 19:32:22
我正在嘗試對使用formidable上傳的文件使用compress_images模塊,但出現“文件不存在”的錯誤。任何想法為什么?這是來自 routes.js 的代碼:app.post('/uploadPicture', isLoggedIn, function(req, res) {    const form = formidable({ multiples: true });    form.parse(req, (err, fields, files) => {        if (err) {            next(err);            return;        }        compress_images(URL+files.someExpressFiles.path+"/"+files.someExpressFiles.name                , "public/build"+files.someExpressFiles.path+"/",                 {compress_force: false, statistic: true, autoupdate: true}, false,                {jpg: {engine: 'mozjpeg', command: ['-quality', '60']}},                {png: {engine: 'pngquant', command: ['--quality=20-50']}},                {svg: {engine: 'svgo', command: '--multipass'}},                {gif: {engine: 'gifsicle', command: ['--colors', '64', '--use-col=web']}}            , function(error, completed, statistic){            console.log('-------------');            console.log(error);            console.log(completed);            res.json(statistic);            console.log('-------------');                                           });        res.json({ fields, files });    });});
查看完整描述

1 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

我通過修復路徑并保留文件擴展名解決了這個問題。這是工作代碼:


app.post('/uploadPicture', isLoggedIn, function(req, res) {

    const form = formidable({ multiples: true

         , keepExtensions: true

         , uploadDir: uploadDir

    });

    form.parse(req, (err, fields, files) => {

        if (err) {

            next(err);

            return;

        }

        var mypath = files.someExpressFiles.path;

        compress_images(mypath

                , "public/build/", 

                {compress_force: false, statistic: true, autoupdate: true}, false,

                {jpg: {engine: 'mozjpeg', command: ['-quality', '60']}},

                {png: {engine: 'pngquant', command: ['--quality=20-50']}},

                {svg: {engine: 'svgo', command: '--multipass'}},

                {gif: {engine: 'gifsicle', command: ['--colors', '64', '--use-col=web']}}

            , function(error, completed, statistic){

            console.log('-------------');

            console.log(error);

            console.log(completed);

            console.log(statistic);

            console.log('-------------');                                   

        });

        res.json({ fields, files });

    });

});


查看完整回答
反對 回復 2022-10-13
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號