NodeJS 如何在Cloudinary上上传PDF文档

3okqufwl  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(101)

我试图通过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" });
  }
};

91zkwejq

91zkwejq1#

您是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)});

字符串

相关问题