【问题标题】:How to save pdf file in node js with express and multer如何使用 express 和 multer 在节点 js 中保存 pdf 文件
【发布时间】:2020-09-12 01:01:10
【问题描述】:

我正在使用multer api,并且我正在将文件保存在内存中,因为我需要上传图像和pdf,并且我需要处理图像。

我可以上传图片,但我不能上传 pdfs 文件。

谢谢

【问题讨论】:

    标签: node.js express multer


    【解决方案1】:

    嗯,这是我的代码...我真的希望对你有所帮助。

    const express = require('express');
    const multer = require('multer');
    const app = express();
    
    const storage =
        multer.diskStorage({
        destination: path.join(__dirname, 'public/img/'), // destination folder
        filename: (req, file, cb) => {
            cb(null, uuid.v4() + path.extname(file.originalname));
        }
    });
    
    const upload =
    multer({
    storage,
    dest: path.join(__dirname, 'public/img/'), // destination folder
    limits: {fileSize: 3500000}, // size we will acept, not bigger
    fileFilter: (req, file, cb) => {
        const filetypes = /jpeg|jpg|png|gif|pdf/; // filetypes you will accept
        const mimetype = filetypes.test(file.mimetype); // verify file is == filetypes you will accept
        const extname = filetypes.test(path.extname(file.originalname)); // extract the file extension
        // if mimetype && extname are true, then no error
        if(mimetype && extname){
            return cb(null, true);
        }
        // if mimetype or extname false, give an error of compatibilty
        return cb("The uploaded file, isn't compatible :( we're sorry");
    }
    }).single('image'); // This is the field where is the input type="file", we only accept 1 image
    app.use(upload);
    

    视图将如下所示

    app.post('/add', async (req, res) => {
    const { originalname, mimetype, destination, filename, path, size} = req.file;
    const newImage = {
        originalname,
        mimetype,
        destination,
        filename,
        path,
        size
    };
    await pool.query('Insert Into imagen set ?', [newImage]);
    res.send('image uploaded');
    });
    

    【讨论】:

    • 池查询有什么作用?
    • 哦,很抱歉,我忘了添加那个代码...运行查询,我最近会添加这个,我不在我的电脑这里:(. const newImage's fields , 是数据库表中的列。例如 mydb.image(originalname, mimetype...)
    【解决方案2】:

    这可能对你有帮助

    // Set storage engine
    const storage = multer.diskStorage({
      destination: "./uploads",
      filename: function(req, file, cb) {
        // null as first argument means no error
        cb(null, Date.now() + "-" + file.originalname);
      },
    });
    
    
    
      let encode_file = null;
      let fileName = "";
      if (req.file) {
        fileName = req.file.originalname;
        var filepath = path.join(__dirname, req.file.path);
        console.log(filepath);
        var stream = fs.readFileSync(filepath);
        encode_file = stream.toString("base64");
      }
    

    【讨论】:

    • 这样,我可以上传文件,PDF和img,。
    • 但是,我想在保存之前处理图像
    • 请问您要执行什么图像处理,以便我考虑一下..
    猜你喜欢
    • 2017-02-05
    • 2016-06-25
    • 1970-01-01
    • 2017-08-30
    • 2020-05-21
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多