1

I need to allow a user to specify an image that needs to be uploaded to the Azure DB instead of hard coding the Image Path like all the tutorials out there. This is what I have so far:

<asp:FileUpload ID="imageUploader" runat="server" />
<asp:Button ID="uploadBtn" runat="server" Text="Upload Image" class="btn btn-success btn-sm" OnClick="uploadBtn_Click" />

Code:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

 // Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference("pictures");

// Create the container if it doesn't already exist.
container.CreateIfNotExists();
container.SetPermissions(new BlobContainerPermissions
{
        PublicAccess = BlobContainerPublicAccessType.Blob
});

CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

blockBlob.UploadFromFile(imageUploader.PostedFile.FileName, FileMode.Open);

However this doesn't seem to work. How can I upload an image to azure using a File Uploader in ASP.NET?

1 Answer 1

3

Your UploadFromFile is using the wrong parameter. See Microsoft CloudBlockBlob.UploadFromFile. The first parameter should be a file path i.e. C:\Users\etc... not just the name of the file that you are parsing imageUploader.PostedFile.FileName.

You most likely want to be using UploadFromStream like this:

blockBlob.UploadFromStream(imageUploader.PostedFile.InputStream);

You should check the documentation for CloudBlockBlob as there are a number of different upload methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm it shows an error when I use that code Ashley: 'System.Web.HttpPostedFile' does not contain a definition for 'FileContent' and no extension method 'FileContent' accepting a first argument of type 'System.Web.HttpPostedFile' could be found (are you missing a using directive or an assembly reference?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.