首页 > 解决方案 > 将不在我计算机上的图像上传到 Cloudinary

问题描述

我正在尝试使用 nodejs 将从我创建的仪表板中选择的图像上传到 Cloudinary。我在这方面遇到了一些麻烦,因为我需要允许用户基本上从他们的计算机中选择任何图像并上传它,而不用硬编码它将进入的文件夹路径。我现在设置它的方式只让我选择我的计算机中具有指定路径的文件。我的前端非常简单:

<label for="image">SELECT IMAGE</label>
<input type="file" name="image" id="fileUploader" required/>

我的后端直接来自 Cloudinary,进行了一些简单的修改以适应我现有的代码,该代码将图像 url 上传到 DB2(sql db):

app.get('/addProducts', (req, res) => {
if (userAuth == 'true') {
    var path = require('path')

        // collected image from a user
        const data = {
            image: req.query.image,
          }
          console.log(data.image)
          // upload image here
          cloudinary.uploader.upload(data.image)
          .then((result) => {
              
              console.log(result.secure_url)
              
             (sql) ......

此行将cloudinary.uploader.upload(data.image)导致出现有关未找到路径的错误。一旦我将它放入代码行中, cloudinary.uploader.upload('./public/css/img/ + data.image)它就可以工作。这不可能发生,因为正如我之前所说,它不允许任何用户从他们的本地计算机中选择图像并上传它。

谢谢您的帮助。

标签: htmlnode.jscloudcloudinary

解决方案


您可以使用 Formidable 或 Multer 和 Express.js 从本地文件系统上传文件并将其上传到 Node.js 中的 Cloudinary。这是一篇博客文章,分步概述并使用 express/multer:https ://cloudinary.com/blog/node_js_file_upload_to_a_local_server_or_to_the_cloud


推荐阅读