简体   繁体   中英

“File does not exist” when using compress_image on formidable upload

I'm trying to use the compress_images module on a file uploaded using formidable , but I'm getting an error that "file does not exist." Any ideas why? Here is the code from 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 });
    });
});

I solved this problem by fixing the paths and keeping the file extension. Here is the working code:

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 });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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