How do I upload a picture to Windows Azure Website

0 votes

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 picture in a directory located at /pictures/uploaded.

Up to this point, my app has been hosted in a Virtual Machine. I was able to do the above by using the following:

string path = ConfigurationManager.AppSettings["rootWebDirectory"] + "/pictures/uploaded;

// Get the file path
if (Directory.Exists(path) == false)
  Directory.CreateDirectory(path);

string filePath = path + "/uploaded" + DateTime.UtcNow.Milliseconds + ".png";
filePath = filePath.Replace("/", "\\").Replace("\\\\", "\\");

// Write the picture to the file system
byte[] bytes = GetPictureBytes();
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
  fileStream.Write(bytes, 0, bytes.Length);
  fileStream.Flush();
  fileStream.Close();
}

Currently, ConfigurationManager.AppSettings["rootWebDirectory"] points to an absolute path. I belive this is where my problem lies. I can't figure out how to switch all of this to a relative path. How do I upload a picture to a relative path within my app hosted on Windows Azure?

Jun 28, 2019 in Azure by sabby
• 4,370 points
1,649 views

1 answer to this question.

0 votes

Check out this example:

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

 <h2>Index</h2>
 <form action="@Url.Action("Upload")" method="post" enctype="multipart/form-data">

     <label for="file">Filename:</label>
     <input type="file" name="file" id="file1" />
     <br />
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file2" />
     <br />
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file3" />
     <br />
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file4" />
     <br />
     <input type="submit" value="Submit" />

 </form>

 @foreach (var item in Model) {

     <img src="@item" alt="Alternate text"/>
  }

Your controller action:

public ActionResult Upload(IEnumerable<HttpPostedFileBase> 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; 

         /// <summary>
         /// Receives the users Id for where the pictures are and creates 
         /// a blob storage with that name if it does not exist.
         /// </summary>
         /// <param name="imageDirecoryUrl"></param>
         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<HttpPostedFileBase> 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<string> GetBlobs()
         {
             // Create the blob client. 
             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

             // Retrieve reference to a previously created container.
             CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);

             List<string> blobs = new List<string>();

             // 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;
         }
     }
answered Jun 28, 2019 by Perry
• 17,100 points

Related Questions In Azure

+1 vote
2 answers

How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

Azure IoT Hub comes with 3 SDK's: Service SDK Device ...READ MORE

answered Apr 13, 2018 in Azure by null_void
• 3,220 points
2,208 views
0 votes
2 answers

How can I download a .vhd image to my local machine from azure and upload the same to a different azure account?

From the Windows Azure Portal you can ...READ MORE

answered Aug 20, 2018 in Azure by Priyaj
• 58,020 points
13,985 views
0 votes
1 answer

How to set default Web page for a website on Microsoft Windows Azure?

This blog post should resolve your issue: http://blogs.msdn.com/b/cesardelatorre/archive/2010/07/22/how-to-set-a-default-page-to-a-windows-azure-web-role-app-silverlight-asp-net-etc.aspx Look ...READ MORE

answered Mar 27, 2019 in Azure by Prerna
• 1,960 points
2,960 views
0 votes
1 answer

How to install Windows Azure Storage Emulator?

There may be an issue with the ...READ MORE

answered Mar 7, 2019 in Azure by Prerna
• 1,960 points
2,787 views
0 votes
1 answer

Azure Storage Emulator shows error.

Check if you are running BitTorrent/uTorrent or similar ...READ MORE

answered Mar 16, 2019 in Azure by Prerna
• 1,960 points
1,709 views
0 votes
1 answer

Friendly filename when downloading Azure blob.

Azure blobs can be downloaded in four ...READ MORE

answered Mar 16, 2019 in Azure by Prerna
• 1,960 points
3,766 views
0 votes
1 answer

Azure: Getting error in loading 'Microsoft.TeamFoundation.WorkItemTracking.Client.Cache'

The error is occurring because you are trying ...READ MORE

answered Mar 26, 2019 in Azure by Prerna
• 1,960 points
964 views
0 votes
1 answer

How to create a sub-container in Azure?

You can create a container named "content" and ...READ MORE

answered Jun 28, 2019 in Azure by Perry
• 17,100 points

edited Oct 7, 2021 by Sarfaraz 1,953 views
0 votes
1 answer

How to Schedule a task in Azure Worker role?

You can simply have a message on ...READ MORE

answered Jul 1, 2019 in Azure by Perry
• 17,100 points
1,427 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP