Exemple #1
0
        static void Main(string[] args)
        {
            string filename = string.Empty;
            bool showHelp = false;

            var options = new OptionSet()
                              {
                                  { "filename=|f=", "File to upload",(v) => filename = v },
                                  { "help|h", "Show help",(v) => showHelp = true }
                              };
            options.Parse(args);

            if(string.IsNullOrEmpty(filename) || showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (!File.Exists(filename))
            {
                Console.WriteLine("File does not exist. Exiting...");
                return;
            }

            var settings = ConfigurationManager.AppSettings;

            var configuration = new AccountConfiguration(settings["cloudinary.cloud"],
                                                         settings["cloudinary.apikey"],
                                                         settings["cloudinary.apisecret"]);

            var uploader = new Uploader(configuration);

            string publicId = Path.GetFileNameWithoutExtension(filename);

            using(var stream =new FileStream(filename, FileMode.Open))
            {
                var uploadResult = uploader.Upload(new UploadInformation(filename, stream)
                                    {
                                        PublicId = publicId,
                                        Transformation = new Transformation(120, 120)
                                                             {
                                                                 Crop = CropMode.Scale
                                                             },
                                        Eager = new[]
                                                    {
                                                        new Transformation(240, 240),
                                                        new Transformation(120, 360) { Crop = CropMode.Limit },
                                                    }
                                    });

                Console.WriteLine("Version: {0}, PublicId {1}", uploadResult.Version, uploadResult.PublicId);
                Console.WriteLine("Url: {0}", uploadResult.Url);
            }
            Console.WriteLine("Successfully uploaded file");

            uploader.Destroy(publicId);

            if(Debugger.IsAttached)
                Console.ReadLine();
        }
 private string UploadToCloudinary(HttpPostedFileBase file, string filename)
 {
     var settings = ConfigurationManager.AppSettings;
     var configuration = new AccountConfiguration(settings["Cloudinary.CloudName"],
                                                  settings["Cloudinary.ApiKey"],
                                                  settings["Cloudinary.ApiSecret"]);
     var uploader = new Uploader(configuration);
     string publicId = Path.GetFileNameWithoutExtension(filename);
     // Upload the file
     var destroyResult = uploader.Destroy(publicId);
     var uploadResult = uploader.Upload(new UploadInformation(filename, file.InputStream)
                         {
                             // explicitly specify a public id (optional)
                             PublicId = publicId,
                             // set the format, (default is jpg)
                             Format = "png",
                             // Specify some eager transformations
                             Eager = new[]
                             {
                                 new Transformation(100, 100) { Format = "png", Crop = CropMode.Thumb, Gravity = Gravity.Face, Radius = 8 }, //, Angle = new Angle(90)
                             }
                         });
     return uploadResult.Version;
 }