如何在cloudinary cdn中保存图像特定文件夹?

时间:2018-02-20 03:12:02

标签: image-uploading cloudinary

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

here is my code to save without folder

my cloudinary cnd folder directory

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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-