【问题标题】:Multiple photos upload via Cloudinary.DotNet通过 Cloudinary.DotNet 上传多张照片
【发布时间】:2020-05-21 15:28:27
【问题描述】:

我已将我的 CloudinaryService 配置为在 cloudinary 上的云上仅上传一张照片。但是我在配置它以使其适用于多次上传时遇到了很大的麻烦。请帮助我,这是我的单次上传代码:

        public async Task<string> UploadPictureAsync(IFormFile pictureFile, string fileName)
        {
            byte[] destinationData;

            using (var ms = new MemoryStream())
            {
                await pictureFile.CopyToAsync(ms);
                destinationData = ms.ToArray();
            }

            UploadResult uploadResult = null;

            using (var ms = new MemoryStream(destinationData))
            {
                ImageUploadParams uploadParams = new ImageUploadParams
                {

                    Folder = "cars",
                    File = new FileDescription(fileName, ms), 
                    PublicId = "audizone"

                };

                uploadResult = this.cloudinaryUtility.Upload(uploadParams);
            }

            return uploadResult?.SecureUri.AbsoluteUri;
        }
       }

}


I change IFormFile pictureFile to List<IFormFile> pictureFiles, going on with foreach (file in pictureFiles)...the only thing this service is doing is just uploading 2 or 3 times the same picture(the first one of three or two)...just not uploading two or three different photos. 




 <form asp-action="Create" method="post" enctype="multipart/form-data">
                    <input type="file" multiple
                           class="form-control text-primary text-center"
                           id="picture"
                           name="picture"
                           placeholder="Picture..." />

     <input type="submit" value="Submit" class="btn btn-dark" style="border-bottom-left- 
     radius:25%;border-bottom-right-radius:25%" />

</form>    

【问题讨论】:

    标签: image upload photo cloudinary


    【解决方案1】:

    我设法使用这种方法成功循环:

    public static void BulkUpload(List<string> filePaths, ResourceType resourceType = ResourceType.Image, string type = "upload")
            {           
                var cloudinary = GetCloudinary(); // Initializing Cloudinary
    
                foreach (var path in filePaths)
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    var streamed = "streamed";
    
                    using (MemoryStream memoryStream = new MemoryStream(bytes))
                    {
                        ImageUploadParams uploadParams = new ImageUploadParams()
                        {
                            File = new FileDescription(streamed, memoryStream)                      
                        };
    
                        ImageUploadResult uploadResult = cloudinary.Upload(uploadParams);
    
                        if (uploadResult.StatusCode == HttpStatusCode.OK)
                            Console.WriteLine("uploaded: " + uploadResult.PublicId);
                        else
                            Console.WriteLine("Failed: " + uploadResult.Error);
                    }
                }
            }
    

    【讨论】:

    • 谢谢你的帮助,但......我恐怕我无法将它实现到我的代码......在我的 创建(List 图片)和多张照片来自 ....我无法处理 List 文件路径... :(
    • 但是我让它为我自己工作...非常感谢谢谢...现在我必须将 url 分配给对象集合...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2015-06-18
    相关资源
    最近更新 更多