使用磁盘存储设备时可以访问文件缓冲区吗?

时间:2019-01-05 17:03:19

标签: node.js express multer

根据文档,如果我要使用buffer属性,则必须使用内存存储。我需要buffer属性,以便可以从文件中读取并导入到DB中。我也想使用磁盘存储,以便将所有上传的文件保存在某个地方。

///routes
var express = require("express"),
    router  = express.Router(),
    multer = require("multer"),
    middleware = require("./middleware.js"),  
    storage = multer.diskStorage({
    destination: function(req, file, next)
    {
        next(null, 'DESTINATION');
    },
    filename: function (req, file, next) {
        next(null, "-" + Date.now());
    }
    }),
    upload = multer({storage: storage }),
    uploadDB = require('./upload.js');
    
    router.post('/upload', [upload.single("file"), uploadDB.post], function(req, res, next) {
    console.log(req.file);
    });

module.exports = router;

////upload.js
var csv = require("fast-csv"),
    mongoose = require("mongoose"),
    Someri = require("../models/somer");

exports.post = function(req, res)
{
    if(!req.file)
    {
        return res.status(400).send("No files were uploaded.");
    }
    var fisier = req.file.buffer;
    console.log(req.file.buffer);
    var someri = [];
    csv
    .fromString(fisier.data.toString(), {
        headers: true,
        ignoreEmpty: true
    })
    .on("data", function(data) {
        data['_id'] = new mongoose.Types.ObjectId();
        someri.push(data);
    })
    .on("end", function()
    {
        Someri.create(someri, function(err, documents)
        {
            if(err) {
                throw(err);
            }
        })
        res.send(someri.length + " someri au fost adaugati.");
    });
}

有没有办法做到这一点?我的想法是从文件的缓冲区读取数据并输入数据,但是由于我正在使用diskStorage,因此无法访问此缓冲区。

0 个答案:

没有答案