How to upload blob to azure using ASP.Net / Tool for upload files to azure using ASP.Net,C#
Simple way to Upload files to azure blob
When we are dealing with windows azure application in ASP.Net we need to communicate with blobs in the azure from the asp.net code. Here we are going to demonstrate how we can upload files from local system to windows azure storage space using asp.net code. The application offers user to upload all files in the mapped local folder to a blob in the azure. It is a usefull tool for working with windows azure storage for upload files.
Requirements need to upload files in to azure blob
For upload files from local folder to azure we must need a azure storage account name and account key. We can purchase it from Microsfot.
Steps to upload files into azure storage
- Create a simple windows application from visual studio, file >> new project >> visual C# >> Windows >> Windows Forms Application
- Need to add reference ‘Microsoft.WindowsAzure.StorageClient.dll’ that we can available after installing
- Drag a Button(For upload), a textbox (For display status) and a ListBox (List files with error or success message)to the form.
- On Button Click function we are going to upload file from local folder (here C:\\myfiles we can configure by accepting browse option).
UI Design for the upload file to Azure Storage application
C# Source Code for Upload files to Azure Storage
private void button1_Click(object sender, EventArgs e) { if (txtFolderName.Text == "") { MessageBox.Show("Please select a folder to upload files"); return; } if (MessageBox.Show("Do you want to Upload files to Blob?", "Confirm Upload", MessageBoxButtons.YesNo) == DialogResult.Yes) { lblHeader.Visible = false; txtStatusMessage.Text = "Upload Starting..."; lstBxFiles.Items.Clear(); UploadFileIntoBlop(txtFolderName.Text.Trim()); lblHeader.Visible = true; } } public void UploadFileIntoBlop(string fileFullPath) { try { string dataCenterSettingKey = "DefaultEndpointsProtocol=" + "http" + ";AccountName=" + "YourAccountNameHere" + ";AccountKey=" + "YourAccountKeyHere"; string filepath = ""; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(dataCenterSettingKey); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); string csvfileblob = "migrationfiles"; CloudBlobContainer blobContainer = blobClient.GetContainerReference(csvfileblob); bool isError = false; foreach (string file in Directory.GetFiles(fileFullPath)) { string[] filename = file.Split('\\'); string theFile = filename[filename.Length - 1]; try { DateTime startDate = DateTime.Now; filepath = file.Trim(); FileStream fileContent = ReadFile(filepath); var blob = blobContainer.GetBlobReference(theFile); blob.UploadFromStream(fileContent); TimeSpan difference = DateTime.Now.Subtract(startDate); int totalSeconds = (int)Math.Ceiling(difference.TotalMinutes); lstBxFiles.Items.Add(theFile + " _Success (" + totalSeconds + " mnts)"); } catch (Exception ex) { isError = true; lstBxFiles.Items.Add(theFile + " _Error"); } } if (isError) txtStatusMessage.Text = "Process completed with errors"; else txtStatusMessage.Text = "Upload done successfully"; } catch (Exception ex) { txtStatusMessage.Text = "Eror Ocurred : " + ex.Message; } } public FileStream ReadFile(string fileFullPath) { FileStream fileStream = null; if (System.IO.File.Exists(fileFullPath)) { fileStream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read); } return fileStream; } private void btnBrowse_Click(object sender, EventArgs e) { DialogResult result = this.folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { txtFolderName.Text = this.folderBrowserDialog1.SelectedPath; } }
Run the application, then press upload button then in the button click event, we are calling UploadFileIntoBlop function. Then it will take all files under the local folder (folder path can be passed as parameter), and uploaded to a container (here it is migration files). In the middle of uploading to blob, we are adding all file names under the mentioned local folder in to list box with the status of whether it is uploaded or not. We can use this application as the tool for upload files from local to azure blob storage.
Leave a Reply Cancel reply
Search other topic from here
Blog Stats
- 291,654 hits
Recent Posts
- How to access all types of server controls in ASP.Net using Javscript/JQuery OR Clear all server controls values using javascript or Jquery
- How to implement Cache mechanism in ASP.Net/C# using simple example OR What is Output Caching and Fragment Caching in ASP.Net/C#
- How to implement CAPTCHA image validation in ASP.Net/C# OR CAPTCHA image validator Sample in ASP.Net/C#
- How to implement SQL Bulk Copy in SQL Server OR Bulk insert into SQL Server using SQL BulkCopy
- How to merge two data tables in ASP.Net/C# OR Merge 2 DataTables and store in a new one in ASP.Net/C#
- How to implement Password validation in ASP.Net/C# OR Implement Password strength using Jquery in ASP.Net
- How to create breadcrumbs in ASP.Net/C# OR Show Navigations for each pages in ASP.Net/C# OR How to implement Sitemap in ASP.Net
- How to create a drag able and resizable div in ASP.Net/C# OR How to make a div Dragable and Resizable using Jquery in ASP.Net/C#
- How to implement BalloonPopupExtender in ASP.Net/C# OR Ballon Popup Extender Sample in ASP.Net/C#
- How to create always visible div using Ajax/ Always visible div in ASP.Net using Ajax
- Example for All Types of SQL JOIN (Inner Join, Cross Join, Outer Join, Self Join)
- How to Create a Data Table Dynamically with sample data and Bind to Grid/Create datatable with sample data.
Archives
- June 2013 (1)
- May 2013 (2)
- October 2012 (4)
- September 2012 (6)
- July 2012 (4)
- June 2012 (1)
- May 2012 (1)
- March 2012 (2)
- February 2012 (2)
- January 2012 (1)
- October 2011 (2)
- September 2011 (5)
- August 2011 (1)
- July 2011 (6)
- June 2011 (18)
- May 2011 (14)
I’m just getting started with my first Azure app. Here’s something which I wanted to implement in my first application:
1. The first page allows various users to log in, each user given their own username.
2. Once they’ve logged in, they would be able to upload their documents which will be stored in the Windows Azure Blob. Each document would have a particular id.
3. All the documents which have been uploaded on the Blob will be visible to all the users. However, if a user wishes to edit a particular document, he will need the document id to access it.
4. Once he’s given access, he’ll be able to edit the document just like Google docs and save changes after he’s done.
5. The last feature is something which will depend on how quickly I get the first 4 steps done. I was earlier thinking of collaborative editing but that would be a little out of my scope right now as I’m a beginner. An alternative to this would be to restrict access to a user if he’s trying to open a document which has already been opened by some other user.
I would like to implement all of this on Microsoft Visual Studio 2012 using Visual C# and ASP.net. I’ve already created the free Azure cloud.
Please let me know how to get started or if any of you know any useful links which I can refer to.
Thanks a lot.
Hi Pallavi,
Thank you for visiting our blog.
A high level implementation of your requirements are given below.
1. You have to create a table for ‘users’ in the database (You didnt mention your database) and create a page for login as a normal application page. Only users having username and password in the table ‘users’ can access the application
2.For uploading logged in users, you can use same code that we posted here.
3. By giving access key and access password you can listed out the name of all blobs in the grid view or any other controls.
4.To obtain the edit functionality, better is first download docs from the blob and gives an option to edit the doc from local, once it edited user can upload file same as step2. Then the same name file will be replaced by new one.
Please go through it and let me know any doubts.
If you want more help shoot details to my email id. infotuvian@gmail.com
Spot on with this write-up, I seriously believe that this web site needs far more attention.
I’ll probably be back again to see more, thanks for the info!