Загрузка нескольких фотографий через Cloudinary.DotNet - PullRequest
0 голосов
/ 05 февраля 2020

Я настроил свой CloudinaryService для загрузки фотографии JUST ONE в мое облако в облачном хранилище. Но у меня действительно большие проблемы с настройкой этого, чтобы он работал на нескольких загрузках. Пожалуйста, помогите мне, вот мой код для разовой загрузки:

        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>    

1 Ответ

0 голосов
/ 06 февраля 2020

Мне удалось успешно l oop, используя этот метод:

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);
                }
            }
        }
...