Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将文件解压缩到磁盘并保存到数据库的路径_Javascript_Node.js_Mongodb_Function_Callback - Fatal编程技术网

Javascript 如何将文件解压缩到磁盘并保存到数据库的路径

Javascript 如何将文件解压缩到磁盘并保存到数据库的路径,javascript,node.js,mongodb,function,callback,Javascript,Node.js,Mongodb,Function,Callback,我正在尝试创建一个应用程序,用户可以上传一个压缩文件,该应用程序将解压文件并将其保存到磁盘,文件的路径将保存到MongoDB以供以后检索 我很难在一个函数中从表单获取上传、解压缩、保存到磁盘以及将解压缩文件的路径上传到数据库。我真的是一个新手,我正在努力学习回调之类的东西,我找不到任何有效的解决方案 这就是我的函数当前的样子: // Multer is a form handling middleware var storage = multer.diskStorage({ destinat

我正在尝试创建一个应用程序,用户可以上传一个压缩文件,该应用程序将解压文件并将其保存到磁盘,文件的路径将保存到MongoDB以供以后检索

我很难在一个函数中从表单获取上传、解压缩、保存到磁盘以及将解压缩文件的路径上传到数据库。我真的是一个新手,我正在努力学习回调之类的东西,我找不到任何有效的解决方案

这就是我的函数当前的样子:

// Multer is a form handling middleware
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    console.log(file)
    cb(null, './uploads/unzip') 
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
  },
})
const upload = multer({ storage }).single('file'); //this is the 1st func in the route

const unzipp = async (req, res, next) => { //second func in route 
  try {
    const dir = 'uploads/unzipped/';      
    var stream = fs.createReadStream(req.file.path)
    stream.pipe(unzip.Extract({path: dir}))
      .on('entry', function () {
        var fileName = entry.path;
        var type = entry.type;
        var size = entry.size;
        console.log(fileName, type, size)
        if (type.isDirectory) {
           postfile()           //TRYING TO CALL POSTFILE() HERE
           console.log('unzipped and path saved')
        } else {
           res.error('Failed unzipping')
          }
    fs.unlink(req.file.path, function (e) {
        if (e) throw e;
        console.log('successfully deleted '+req.file.path);
    });
  })
  } catch (e) {
    console.error(e)
  }
next();
}

//Upload is a mongoDB cluster Schema
async function postfile () {
  try{ 
  let newUpload = new Upload(req.body); //new instance of uplaod based on the model based on req.body
  newUpload.title = req.body.title;
  newUpload.description = req.body.description;
  newUpload.labels = req.body.labels;
  newUpload.filePath = fileName; //ASSIGN FILEPATH IN DB SCHEMA TO UNZIPPED FILE PATH
  console.log("filePath saved")
  newUpload.save()
    .then(newUpload => {
      res.status(200).json({file: "File added successfully"})
    })
    .catch(err => {
      res.status(400).send('File upload failed to save to DB :(')
    })

  } catch (e) {

    console.error(e);
  }
}
正如您所看到的,我正在尝试调用函数以在解压函数中保存mongo模式。这是单独文件夹中的投递路线:

router.post('/upload', FileCtrl.upload, FileCtrl.unzipp)
  • 我还尝试将解压文件的条目路径保存为全局变量(fileName),并将模式中的路径指定为fileName,但也不起作用:
  • 这将导致错误“ReferenceError:未定义文件名”

  • 新路线如下所示:

我已经尝试解决这个问题很长时间了,非常感谢你的建议

编辑: 出于测试目的,我对文件路径进行了硬编码,并将其保存到数据库中

const postfile = async (req, res) => {
  try{ 
  console.log("Posting to DB")
  //var stream = fs.readdirSync('./uploads/unzipped/Nancy_Collins_118226967_v2')
  let newUpload = new Upload(req.body); //new instance of uplaod based on the model based on req.body
  newUpload.title = req.body.title;
  newUpload.description = req.body.description;
  newUpload.labels = req.body.labels;
  newUpload.filePath = './uploads/unzipped/Nancy_Collins_118226967_v2';
  console.log("Ok so far")
  newUpload.save()
    .then(newUpload => {
      res.status(200).json({file: "File added successfully"})
    })
    .catch(err => {
      res.status(400).send('File upload failed to save to DB :(')
    })

  } catch (e) {

    console.error(e);
  }
}
显然,这既不实际也不动态,但这是可能的

router.post('/upload', FileCtrl.upload, FileCtrl.unzipp, FileCtrl.postfile)
const postfile = async (req, res) => {
  try{ 
  console.log("Posting to DB")
  //var stream = fs.readdirSync('./uploads/unzipped/Nancy_Collins_118226967_v2')
  let newUpload = new Upload(req.body); //new instance of uplaod based on the model based on req.body
  newUpload.title = req.body.title;
  newUpload.description = req.body.description;
  newUpload.labels = req.body.labels;
  newUpload.filePath = './uploads/unzipped/Nancy_Collins_118226967_v2';
  console.log("Ok so far")
  newUpload.save()
    .then(newUpload => {
      res.status(200).json({file: "File added successfully"})
    })
    .catch(err => {
      res.status(400).send('File upload failed to save to DB :(')
    })

  } catch (e) {

    console.error(e);
  }
}