Reputation: 287
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?
Upvotes: 1
Views: 2852
Reputation: 7301
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.
Upvotes: 3