如何在 Cloudinary 上上传 PDF 文档

How to upload a PDF Document on Cloudinary

我正在尝试通过 node.js 服务器在 Cloudinary 上上传 PDF 文档。 我目前使用的方法不充分并且不起作用。 如果有人能提供解决此问题的有效方法,我将不胜感激。

云存储配置:

const cloudinary = require("cloudinary").v2,
  { CloudinaryStorage } = require("multer-storage-cloudinary");  

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});
 
const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  folder: "<some_folder>",
  allowedFormats: ["jpg", "png", "pdf"],
});


module.exports.store = storage;

图片上传路径:

const express = require("express"),
  router = express.Router(),
  cors = require("cors"),
  fileUploadController = require("../../../controllers/haulage/users/fileUploadController");

const authenticate = require("../../../auth/auth"),
  multer = require("multer"),
  storage = require("../../../../config/cloudinary").store,
  parser = multer({ storage: storage });

router
  .route("/images")
  .put(
    authenticate,
    cors(),
    parser.array("image"),
    fileUploadController.uploadImage
  );

module.exports = router;

图片上传控制器:

exports.uploadImage = async (req, res) => {
  try {
    const events = JSON.parse(JSON.stringify(req.files));

    const urls = [];    

    events.map((e) => {
      const url = e.path;
     
      urls.push(url);
    });

    res.status(201).json({ URLS: urls });
  } catch {
    res.status(500).json({ error: "Server Error, Image Failed to Add" });
  }
};

您是否参与了 ​​Cloudinary 的免费计划?如果是,则可能与此问题有关:https://support.cloudinary.com/hc/en-us/articles/360016480179-Why-does-my-pdf-link-isn-t-working- 如果不是,能否详细说明一下这个问题?你在哪里看到错误? 上传pdf的代码(任何图片都一样):

var cloudinary = require('cloudinary')
cloudinary.v2.uploader.upload("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",function(res,err){console.log(res,err)});