Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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 当函数需要路径或链接时,将pdf文件传递给函数_Javascript_Node.js_Pdf.js_Pdf Extraction_Multer Gridfs Storage - Fatal编程技术网

Javascript 当函数需要路径或链接时,将pdf文件传递给函数

Javascript 当函数需要路径或链接时,将pdf文件传递给函数,javascript,node.js,pdf.js,pdf-extraction,multer-gridfs-storage,Javascript,Node.js,Pdf.js,Pdf Extraction,Multer Gridfs Storage,我正在为一个在线图书馆开发一个web应用程序。我想从将要上传的PDF中提取元数据,为此,我使用nodejs库PDF.js-extract和multer-gridfs存储进行上传。问题是我正在接收一个PDF文件(req.file),该函数需要指向PDF文件的路径或链接,因此显示错误 "TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received t

我正在为一个在线图书馆开发一个web应用程序。我想从将要上传的PDF中提取元数据,为此,我使用nodejs库PDF.js-extract和multer-gridfs存储进行上传。问题是我正在接收一个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 });
  });
});
(编辑澄清)我正在使用multer和gridFS将文件上传到mongoose

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 });
      });
  })
});
根据,您可以使用
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 });
  });
});
编辑:我刚刚阅读了,有一个名为
preservePath
的选项

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


编辑2:我认为您需要使用从数据库中提取文件,然后将其转换为缓冲区(如在线程中),然后使用PDFExtract函数。

您不能将文件缓冲到函数中吗?检查一下,我正在研究它,但看起来readFile和readFileSync也将路径、字符串或缓冲区作为参数。我用它得到了同样的错误。奇怪的是,req.file.path没有定义。对req.file执行console.log会给出
{fieldname:'file',originalname:'Alice_in_Wonderland.pdf',编码:'7bit',mimetype:'application/pdf',id:5cd1528c0614dec8f5774,文件名:'3c90b9cfa1925acf4d75d629e59099c.pdf',元数据:null,bucketName:'uploads',chunkSize:26120,size:3083601,md5:'22f3af3730bc9820c1bf6d90b327a47',上传日期:2019-05-07T09:40:32.796Z,contentType:'application/pdf'}
这真的很奇怪……你能告诉我们如何初始化multer吗?你使用的是multer的最新版本吗?我刚刚阅读了,有一个名为
preservePath
的选项,请尝试将其设置为true。@LuisdelaCalI编辑了我的问题,我也对multer使用了gridfs。库multer gridfs-storage@LuisdelaCal我不复杂当然可以,但是您不能在
fileInfo
中添加一个值为
file.path
path
属性吗?