简体   繁体   中英

Uploading Image to Azure Blob using File Uploader in ASP.NET?

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?

Your UploadFromFile is using the wrong parameter. See Microsoft CloudBlockBlob.UploadFromFile . The first parameter should be a file path ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM