【问题标题】:How to save an image to a specific folder with Cloudinary?如何使用 Cloudinary 将图像保存到特定文件夹?
【发布时间】:2018-07-30 08:16:37
【问题描述】:

我想将图像文件保存在 Cloudinary 上的特定文件夹中。文件夹名称为“userimg”。

Here is my code to save without a folder.

My cloudinary and folder directory.

【问题讨论】:

    标签: image-uploading cloudinary


    【解决方案1】:

    Cloudinary Upload API 支持folder 参数。见-https://cloudinary.com/documentation/image_upload_api_reference#upload

    请尝试添加:

    formData.append('folder', 'userimg');
    

    您还可以在公共 ID 中包含文件夹,例如“myfolder/myimageid”。

    另见:

    https://support.cloudinary.com/hc/en-us/articles/202520902-Can-I-create-folders-in-my-Cloudinary-account-

    【讨论】:

      【解决方案2】:

      在cloudinary的特定文件夹中保存图片文件的代码:

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
      <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
      <div class="container">
          <div class="card">
              <img src="http://fillmurray.com/g/300/300" id="img-preview" />
              <label class="file-upload-container" for="file-upload">
                  <input id="file-upload" type="file">
                  Select an Image
              </label>
          </div>
      </div>
      <script>
          var cloudeinary_url = "your_cloudeinary_url";
          var upload_presets = "your_upload_presets";
      
          var ingpriview = document.getElementById("img-preview");
          var fileupload = document.getElementById("file-upload");
      
          fileupload.addEventListener('change',
              function (event) {
                  var files = event.target.files[0];
                  var formData = new FormData();
                  formData.append('file', files);
                  formData.append('upload_preset', upload_presets);
                  formData.append('folder', 'itemimg'); // itemimg is folder name
      
                  axios({
                      url: cloudeinary_url,
                      method: 'POST',
                      header: {
                          'Content-Type': 'application/x-www-form-urlencoded'
                      },
                      data: formData
                  }).then(function (res) {
                      document.getElementById("img-preview").src = res.data.secure_url;
                      console.log(res);
                  }).catch(function (err) {
                      console.log(err);
                  });
              });
      </script>
      

      【讨论】:

        猜你喜欢
        • 2023-02-13
        • 1970-01-01
        • 2023-03-23
        • 2011-11-08
        • 1970-01-01
        • 2019-08-25
        • 2017-05-25
        • 2013-08-03
        • 2019-01-25
        相关资源
        最近更新 更多