Exemplo n.º 1
0
        public static void UploadImage(IImageFile imageFile, MultipartFileData file = null)
        {
            try
            {
                if (GetContainers().ToList().Find(itm => itm == imageFile.GetContainer()) != null)
                {
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
                    CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer  container      = blobClient.GetContainerReference(imageFile.GetContainer());
                    CloudBlockBlob      imageBlob      = container.GetBlockBlobReference(imageFile.GetFileName());

                    if (file != null)
                    {
                        // Data is in local upload folder
                        imageBlob.UploadFromFile(file.LocalFileName, FileMode.Open);
                    }
                    else
                    {
                        // Data is in imageFile FileData
                        imageBlob.UploadFromByteArray(imageFile.GetFileData(), 0, imageFile.GetFileSize() - 1);
                    }
                }
                else
                {
                    throw new Exception("Container not found");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }