ASP.NET framework 4.8 code for file uploding into Azure storage/container using FileUpload control

Chandrakant Pawle 101 Reputation points
2024-04-29T17:12:45.4533333+00:00

Hello

For file uploading into Azure Storage / containers using the FileUpload control—preferably upon button click—ASP.NET framework 4.8 code is required.

Thanks in advance

Regards

Chandrakant

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,905 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,217 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anand Prakash Yadav 7,795 Reputation points Microsoft Vendor
    2024-05-02T07:59:19.8166667+00:00

    Hello Chandrakant Pawle,

    Thank you for posting your query here!

    First, you need to make sure that you have the Azure.Storage.Blobs NuGet package installed:

    You can install it via NuGet Package Manager in Visual Studio.

    Install-Package Azure.Storage.Blobs
    

    Can you please try with the following updated code and let us know if it helps:

    using Azure.Storage.Blobs;
    using System;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Retrieve the connection string from configuration (e.g., Web.config)
            string storageConnectionString = System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"];
    
            // Name of your container
            string containerName = "your-container-name";
    
            // Create a BlobServiceClient
            BlobServiceClient blobServiceClient = new BlobServiceClient(storageConnectionString);
    
            // Get the container client object
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    
            // Create the container if it does not exist
            containerClient.CreateIfNotExists();
    
            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(Path.GetFileName(FileUploadControl.FileName));
    
            if (FileUploadControl.HasFile && FileUploadControl.PostedFile.ContentLength > 0)
            {
                // Upload the file content
                using (Stream uploadFileStream = FileUploadControl.PostedFile.InputStream)
                {
                    blobClient.Upload(uploadFileStream, overwrite: true);
                }
    
                // Provide feedback to the user
                UploadStatusLabel.Text = "File uploaded successfully!";
            }
            else
            {
                // Inform user that a file was not selected or file is empty
                UploadStatusLabel.Text = "Please select a file to upload.";
            }
        }
        catch (Exception ex)
        {
            // Handle any errors that occurred during upload
            UploadStatusLabel.Text = "Upload failed. Error: " + ex.Message;
        }
    }
    

    Do let us know if you have any further queries. I’m happy to assist you further.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Umar 160 Reputation points
    2024-04-30T05:33:48.7366667+00:00

    Certainly! Below is a basic example of how you can upload a file from an ASP.NET FileUpload control to an Azure Storage container using the .NET Framework 4.8:

    
    using Microsoft.Azure.Storage;
    
    using Microsoft.Azure.Storage.Blob;
    
    using System;
    
    using System.IO;
    
    using System.Web.UI.WebControls;
    
    protected void UploadButton_Click(object sender, EventArgs e)
    
    {
    
        // Retrieve the connection string for your Azure Storage account
    
        string storageConnectionString = "YourStorageConnectionString";
    
        // Retrieve reference to the container
    
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
    
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
        CloudBlobContainer container = blobClient.GetContainerReference("your-container-name");
    
        // Create the container if it doesn't exist
    
        container.CreateIfNotExists();
    
        // Retrieve reference to a blob
    
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(Path.GetFileName(FileUploadControl.FileName));
    
        // Upload the file from the FileUpload control to the blob
    
        using (Stream fileStream = FileUploadControl.FileContent)
    
        {
    
            blockBlob.UploadFromStream(fileStream);
    
        }
    
        // Optionally, provide feedback to the user upon successful upload
    
        UploadStatusLabel.Text = "File uploaded successfully!";
    
    }
    
    

    Make sure to replace "YourStorageConnectionString" with the actual connection string for your Azure Storage account and "your-container-name" with the name of the container where you want to upload the file.

    This code assumes you have an ASP.NET FileUpload control named FileUploadControl and a Button control named UploadButton on your ASPX page. The UploadButton_Click event handler is triggered upon clicking the button and performs the file upload operation to Azure Storage.

    0 comments No comments

  2. Chandrakant Pawle 101 Reputation points
    2024-04-30T11:37:09.04+00:00

    Thanks Umar for your efforts

    But, I guess Microsoft.Azure.Storage.Blob is old and deprecated as per documentation.

    Hence, using Azure.Storage.blobs libraries in the code as following:

    using Azure.Storage.Blobs;

    using Azure.Storage.Blobs.Models;

    If possible, could you pls help me in sharing same code using above libraries. Thanks

    Regards

    Chandrakant

    0 comments No comments

  3. Chandrakant Pawle 101 Reputation points
    2024-05-02T14:15:18.15+00:00

    Hello Anand

    Thanks for your efforts. The code you shared works perfectly for me

    Thanks again...

    Regards

    Chandrakant

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.