我正在尝试通过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)});
我在 Cloudinary 上传 PDF 文件时遇到了同样的问题。看我的推文我是如何解决的