【问题标题】:Passing a pdf file to a function when it requires a path or link当需要路径或链接时将 pdf 文件传递​​给函数
【发布时间】:2019-09-24 21:48:36
【问题描述】:

我正在为一个在线图书馆开发一个网络应用程序。我想从将要上传的 PDF 中提取元数据,为此我使用 nodejs 库 pdf.js-extract 和 multer-gridfs-storage 进行上传。问题是我收到一个 PDF 文件 (req.file),该函数需要 PDF 文件的路径或链接,因此显示错误

"TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type object"

我想知道是否有办法将文件作为链接传递、将文件临时保存在本地或找到适合我需要的另一个库。

这是我当前的代码。

const PDFExtract  = require('pdf.js-extract').PDFExtract;

app.post('/upload', upload.single('file'), (req, res) => {
  const pdfExtract = new PDFExtract();
  const options = {};

  pdfExtract.extract(req.file, options, (err, data) => {
      if (err){
        res.status(404).send({ message: err });
      }
      res.status(200).send({ message: data });
  });
});

(为澄清而编辑)我正在使用带有 gridFS 的 multer 将文件上传到猫鼬。

const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');

// Create storage engine
const storage = new GridFsStorage({
  url: mongoURI,
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(16, (err, buf) => {
        if (err) {
          return reject(err);
        }
        const filename = buf.toString('hex') + path.extname(file.originalname);
        const fileInfo = {
          filename: filename,
          bucketName: 'uploads'
        };
        resolve(fileInfo);
      });
    });
  }
});
const upload = multer({ storage });

受 Oliver Nybo 启发的解决方案

app.post('/upload', upload.single('file'), (req, res) => {
  const pdfExtract = new PDFExtract();
  const options = {};

  var readableStream = gfs.createReadStream({ filename : req.file.filename });
  var buff;

  var bufferArray = [];
  readableStream.on('data',function(chunk){  
      bufferArray.push(chunk);
  });
  readableStream.on('end',function(){
      var buffer = Buffer.concat(bufferArray);
      buff=buffer;
      pdfExtract.extractBuffer(buff, options, (err, data) => {
        if (err) {
          res.status(404).send({ message: err });
        }
        res.status(200).send({ message: data });
      });
  })
});

【问题讨论】:

  • 你不能只将文件缓冲到函数中吗?检查stackoverflow.com/questions/19705972/…
  • 我正在调查它,但似乎 readFile 和 readFileSync 也将路径、字符串或缓冲区作为参数。我使用它时遇到了同样的错误。

标签: javascript node.js pdf.js pdf-extraction multer-gridfs-storage


【解决方案1】:

根据multer's api documentation,可以使用req.file.path获取上传文件的完整路径。

const PDFExtract  = require('pdf.js-extract').PDFExtract;

app.post('/upload', upload.single('file'), (req, res) => {
  const pdfExtract = new PDFExtract();
  const options = {};

  pdfExtract.extract(req.file.path, options, (err, data) => {
      if (err){
        res.status(404).send({ message: err });
      }
      res.status(200).send({ message: data });
  });
});

编辑:我刚刚阅读了multer options 并且有一个名为preservePath 的选项。

preservePath - 保留文件的完整路径而不仅仅是基本名称

编辑2:我认为你需要使用gridfs-stream从数据库中提取文件,然后将其转换为缓冲区(如this线程中),然后使用PDFExtract的@987654325 @函数。

【讨论】:

  • 奇怪的是,req.file.path 是未定义的。做 req.file 的 console.log 给{ fieldname: 'file', originalname: 'Alice_in_Wonderland.pdf', encoding: '7bit', mimetype: 'application/pdf', id: 5cd1528c0614d139ec8f5774, filename: '3c90b9cfa1925acf4d75d6d629e5909c.pdf', metadata: null, bucketName: 'uploads', chunkSize: 261120, size: 3083601, md5: '22f3af3730bc9820c1bf6d90b3271a47', uploadDate: 2019-05-07T09:40:32.796Z, contentType: 'application/pdf' }
  • 这真的很奇怪......你能告诉我们你是如何初始化 multer 的吗?您使用的是最新版本的 multer 吗?
  • 我刚刚阅读了multer options 并且有一个名为preservePath 的选项,尝试将其设置为true。 @LuisdelaCal
  • 我编辑了我的问题,我也在使用带有 multer 的 gridfs。库 multer-gridfs-storage
  • @LuisdelaCal 我不完全确定,但你不能将path 属性添加到fileInfo,值为file.path
猜你喜欢
  • 2011-02-26
  • 2018-12-26
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2014-08-29
相关资源
最近更新 更多