Upload Picture to Windows Azure Web Site

后端 未结 4 1052
终归单人心
终归单人心 2020-12-08 05:49

I have an ASP.NET MVC 4 app that i want to deploy to Windows Azure. A part of this app involves uploading a picture. When the picture is uploaded, I want to store the pictur

4条回答
  •  独厮守ぢ
    2020-12-08 06:23

    Here is my as easy as it comes description of how to set this up in azure. http://geekswithblogs.net/MagnusKarlsson/archive/2012/12/02/how-to-use-azure-storage-for-images.aspx

    //Edit; heres the complete example from my blog(if the blog dies). yourViewName.cshtml

     @model List  
     @{
         ViewBag.Title = "Index";
     }
    
     

    Index





    @foreach (var item in Model) { Alternate text }

    Your controller action

    public ActionResult Upload(IEnumerable file)
             {
                 BlobHandler bh = new BlobHandler("containername");
                 bh.Upload(file);
                 var blobUris=bh.GetBlobs();
    
                 return RedirectToAction("Index",blobUris);
             }
    

    Your model

     public class BlobHandler
         {
             // Retrieve storage account from connection string.
             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
             CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
             private string imageDirecoryUrl; 
    
             /// 
             /// Receives the users Id for where the pictures are and creates 
             /// a blob storage with that name if it does not exist.
             /// 
             /// 
             public BlobHandler(string imageDirecoryUrl)
             {
                 this.imageDirecoryUrl = imageDirecoryUrl;
                 // Create the blob client.
                 CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
                 // Retrieve a reference to a container. 
                 CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);
    
                 // Create the container if it doesn't already exist.
                 container.CreateIfNotExists();
    
                 //Make available to everyone
                 container.SetPermissions(
                     new BlobContainerPermissions
                     {
                         PublicAccess = BlobContainerPublicAccessType.Blob
                     });
             }
    
             public void Upload(IEnumerable file)
             {
                 // Create the blob client.
                 CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
                 // Retrieve a reference to a container. 
                 CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);
    
                 if (file != null)
                 {
                     foreach (var f in file)
                     {
                         if (f != null)
                         {
                             CloudBlockBlob blockBlob = container.GetBlockBlobReference(f.FileName);
                             blockBlob.UploadFromStream(f.InputStream);
                         }
                     }
                 }
             }
    
             public List GetBlobs()
             {
                 // Create the blob client. 
                 CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
                 // Retrieve reference to a previously created container.
                 CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);
    
                 List blobs = new List();
    
                 // Loop over blobs within the container and output the URI to each of them
                 foreach (var blobItem in container.ListBlobs())
                     blobs.Add(blobItem.Uri.ToString());
    
                 return blobs;
             }
         }
    

提交回复
热议问题