Home
%3CLINGO-SUB%20id%3D%22lingo-sub-287834%22%20slang%3D%22en-US%22%3EUpload%20and%20Download%20files%20from%20Azure%20Storage%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-287834%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2024%2C%202014%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EHere%20is%20sample%20code%20to%20download%20and%20upload%20files%20to%20Azure%20Storage%20from%20Universal%20Windows%20Apps.%3C%2FP%3E%0A%20%20%3CP%3EComplete%20source%20code%20is%20available%20%3CA%20href%3D%22http%3A%2F%2Fcode.msdn.microsoft.com%2FUniversal-Windows-Apps-1ea96689%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%3C%2FA%3E%3C%2FP%3E%0A%20%20%3CH2%20id%3D%22toc-hId-1652185640%22%20id%3D%22toc-hId-1765779806%22%3EIntroduction%20to%20Universal%20Windows%20Apps%3C%2FH2%3E%0A%20%20%3CP%3EVisual%20Studio%20template%20for%20Universal%20Windows%20Apps%20allows%20developers%20to%20build%20Windows%20Store%20App%20and%20Phone%20App%20in%20a%20single%20Visual%20Studio%20solution.%20This%20Visual%20Studio%20solution%20includes%20three%20projects%2C%20project%20for%20design%20and%20feature%20experiences%20unique%20to%20Store%20App%2C%20second%20project%20for%20Phone%20App%20and%20third%20project%20for%20shared%20code.%26nbsp%3B%20For%20more%20details%20on%20Universal%20Apps%20template%20in%20Visual%20Studio%20please%20click%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fvisualstudio%2Farchive%2F2014%2F04%2F14%2Fusing-visual-studio-to-build-universal-xaml-apps.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%3C%2FA%3E%3C%2FP%3E%0A%20%20%3CP%3EHere%20is%20the%20Visual%20Studio%20projects%20for%20Universal%20Windows%20Apps.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60043iFEBDCD86AA28818F%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20project%20ending%20with%20.Windows%20is%20for%20Store%20App%2C%20project%20ending%20with%20.WindowsPhone%20is%20for%20Phone%20App%20and%20.Shared%20is%20for%20shared%20code.%3C%2FP%3E%0A%20%20%3CP%3EIn%20the%20Shared%20Project%2C%20use%20following%20%23defines%20to%20write%20specific%20code%20for%20Phone%20App%20or%20Store%20App%3C%2FP%3E%0A%20%20%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A527ae271-82e7-4f24-ad29-15b9a2110679%22%3E%0A%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20%23if%20WINDOWS_PHONE_APP%20%3CBR%20%2F%3E%20%2F%2F%20For%20Windows%20Phone%20App%20%3CBR%20%2F%3E%20%23elif%20WINDOWS_APP%20%3CBR%20%2F%3E%20%2F%2F%20For%20Windows%20Store%20App%20%3CBR%20%2F%3E%20%23else%20%3CBR%20%2F%3E%20%2F%2F%20Error%20%3CBR%20%2F%3E%20%23endif%20%3CBR%20%2F%3E%3C%2FDIV%3E%0A%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--899971321%22%20id%3D%22toc-hId--786377155%22%3EIntroduction%20to%20Azure%20Storage%3C%2FH2%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EAzure%20Storage%20is%20storage%20in%20cloud%20which%20is%20accessible%20from%20anywhere%20in%20the%20world%2C%20from%20any%20type%20of%20application%2C%20whether%20it%E2%80%99s%20running%20in%20the%20cloud%2C%20on%20the%20desktop%2C%20on%20an%20on-premises%20server%2C%20or%20on%20a%20mobile%20or%20tablet%20device.%20Azure%20Storage%20support%20four%20types%20of%20storage%20%3A%20Blob%20Storage%2C%20Table%20Storage%2C%20Queue%20Storage%20and%20File%20Storage.%20For%20more%20details%20please%20click%20%3CA%20href%3D%22http%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fdocumentation%2Farticles%2Fstorage-introduction%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20.%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-842839014%22%20id%3D%22toc-hId-956433180%22%3ESteps%20to%20create%20Azure%20Storage%3C%2FH2%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3COL%3E%3CBR%20%2F%3E%3CLI%3ELogin%20to%20Azure%20Portal%20%40%20%3CA%20href%3D%22https%3A%2F%2Fmanage.windowsazure.com%2F%22%20title%3D%22https%3A%2F%2Fmanage.windowsazure.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fmanage.windowsazure.com%2F%3C%2FA%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EClick%20on%20New%20at%20the%20bottom%20left%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ESelect%20Data%20Services%20%7C%20Storage%20%7C%20Quick%20Create%20and%20enter%20URL%26nbsp%3B%20as%20shown%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60044i0B8B9323E96AC89E%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENext%20create%20container%2C%20click%20on%20Create%20a%20Container%20in%20the%20Storage%20%7C%20UniversalAppAzureStorage%20%7C%20Container%20tab%20as%20shown%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60045i7011CB90FE2B5F20%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EEnter%20a%20name%20for%20container%20as%20shown%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60046i942DCDC3BE030177%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EClick%20on%20Manage%20Access%20Keys%20in%20the%20Dashboard%20tab%20and%20copy%20the%20Storage%20Account%20Name%20and%20Primary%20Access%20Key%20as%20shown%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60047i03389E5E369C2842%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FOL%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--1709317947%22%20id%3D%22toc-hId--1595723781%22%3ESteps%20to%20create%20Universal%20Windows%20App%3C%2FH2%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3COL%3E%3CBR%20%2F%3E%3CLI%3EOpen%20Visual%20Studio%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EClick%20on%20File%20%7C%20New%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ESelect%20Templates%20%7C%20Visual%20C%23%20%7C%20Store%20Apps%20%7C%20Universal%20Apps%20%7C%20Hub%20App%20as%20shown%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60048i01E9F54D15553883%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20install%20NuGet%20Package%20for%20Azure%20Store%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ERight%20click%20on%20the%20.Windows%20project%20and%20select%20Manage%20NuGet%20Packages%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EIn%20the%20search%20window%20type%20Azure%20Storage%20as%20shown%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60049i3FE98A8385F011D3%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EClick%20on%20Install%20button%20for%20Azure%20Storage%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20install%20the%20Azure%20Storage%20NuGet%20Package%20on%20.WindowsPhone%20project%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ERight%20click%20on%20the%20.WindowsPhone%20project%20and%20select%20Manage%20NuGet%20Packages%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EIn%20the%20search%20window%20type%20Azure%20Storage%20and%20click%20on%20the%20install%20button%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20lets%20right%20code%20for%20uploading%20file%20to%20Azure%20Storage%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EOpen%20App.xaml.cs%20file%20in%20the%20.Shared%20Project%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ECopy%20this%20below%20upload%20function%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3Ab0e2cb2c-8491-4179-8142-6c2a857eca94%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20private%20async%20Task%3CINT%3E%20UploadToAzureStorage()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20try%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20create%20Azure%20Storage%20%3CBR%20%2F%3E%20CloudStorageAccount%20storageAccount%20%3D%20CloudStorageAccount.Parse(%22DefaultEndpointsProtocol%3Dhttps%3BAccountName%3Duniversalappazurestorage%3BAccountKey%3D%3CYOUR%20key%3D%22%22%3E%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20blob%20client.%20%3CBR%20%2F%3E%20CloudBlobClient%20blobClient%20%3D%20storageAccount.CreateCloudBlobClient()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20container%20%3CBR%20%2F%3E%20CloudBlobContainer%20container%20%3D%20blobClient.GetContainerReference(%22containerone%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20block%20blob%20%3CBR%20%2F%3E%20CloudBlockBlob%20blockBlob%20%3D%20container.GetBlockBlobReference(%22filename%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20local%20file%20%3CBR%20%2F%3E%20StorageFile%20file%20%3D%20await%20ApplicationData.Current.LocalFolder.CreateFileAsync(%22filename%22%2C%20CreationCollisionOption.ReplaceExisting)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20copy%20some%20txt%20to%20local%20file%20%3CBR%20%2F%3E%20MemoryStream%20ms%20%3D%20new%20MemoryStream()%3B%20%3CBR%20%2F%3E%20DataContractSerializer%20serializer%20%3D%20new%20DataContractSerializer(typeof(string))%3B%20%3CBR%20%2F%3E%20serializer.WriteObject(ms%2C%20%22Hello%20Azure%20World!!%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20(Stream%20fileStream%20%3D%20await%20file.OpenStreamForWriteAsync())%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ms.Seek(0%2C%20SeekOrigin.Begin)%3B%20%3CBR%20%2F%3E%20await%20ms.CopyToAsync(fileStream)%3B%20%3CBR%20%2F%3E%20await%20fileStream.FlushAsync()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20upload%20to%20Azure%20Storage%20%3CBR%20%2F%3E%20await%20blockBlob.UploadFromFileAsync(file)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%201%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20catch%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20return%20error%20%3CBR%20%2F%3E%20return%200%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%3C%2FYOUR%3E%3C%2FINT%3E%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20copy%20this%20below%20download%20function%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A22eea314-19c9-4cc2-bbb0-7591adec89a6%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20private%20async%20Task%3CINT%3E%20DownloadFromAzureStorage()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20try%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20create%20Azure%20Storage%20%3CBR%20%2F%3E%20CloudStorageAccount%20storageAccount%20%3D%20CloudStorageAccount.Parse(%22DefaultEndpointsProtocol%3Dhttps%3BAccountName%3Duniversalappazurestorage%3BAccountKey%3D%3CYOUR%20key%3D%22%22%3E%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20blob%20client.%20%3CBR%20%2F%3E%20CloudBlobClient%20blobClient%20%3D%20storageAccount.CreateCloudBlobClient()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20container%20%3CBR%20%2F%3E%20CloudBlobContainer%20container%20%3D%20blobClient.GetContainerReference(%22containerone%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20block%20blob%20%3CBR%20%2F%3E%20CloudBlockBlob%20blockBlob%20%3D%20container.GetBlockBlobReference(%22filename%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20create%20a%20local%20file%20%3CBR%20%2F%3E%20StorageFile%20file%20%3D%20await%20ApplicationData.Current.LocalFolder.CreateFileAsync(%22filename_from_azure%22%2C%20CreationCollisionOption.ReplaceExisting)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20download%20from%20Azure%20Storage%20%3CBR%20%2F%3E%20await%20blockBlob.DownloadToFileAsync(file)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%201%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20catch%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20return%20error%20%3CBR%20%2F%3E%20return%200%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%3C%2FYOUR%3E%3C%2FINT%3E%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ENow%20call%20these%20two%20functions%20%3CBR%20%2F%3E%3CDIV%20id%3D%22scid%3A9D7513F9-C04C-4721-824A-2B34F0212519%3A9d2d4308-3aa0-4e5b-a54c-a82368293ebe%22%3E%0A%20%20%20%20%20%3CDIV%3E%3CBR%20%2F%3E%20protected%20async%20override%20void%20OnLaunched(LaunchActivatedEventArgs%20e)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%23if%20DEBUG%20%3CBR%20%2F%3E%20if%20(System.Diagnostics.Debugger.IsAttached)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20this.DebugSettings.EnableFrameRateCounter%20%3D%20true%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%23endif%20%3CBR%20%2F%3E%20int%20r%20%3D%20await%20UploadToAzureStorage()%3B%20%3CBR%20%2F%3E%20if%20(r%20%3D%3D%201)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20await%20DownloadFromAzureStorage()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Frame%20rootFrame%20%3D%20Window.Current.Content%20as%20Frame%3B%3C%2FDIV%3E%0A%20%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ERun%20the%20Windows%20Store%20App%20and%20check%20the%20files%20local%20storage%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ETo%20get%20the%20path%20of%20the%20local%20storage%2C%20look%20at%20the%20Path%20variable%20value%20of%20the%20StorageFile%20as%20shown%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60050i42F043D60F25E135%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fcode.msdn.microsoft.com%2FUniversal-Windows-Apps-1ea96689%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%20%3C%2FA%3E%20is%20the%20complete%20source%20code%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FOL%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-287834%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2024%2C%202014%20%26nbsp%3B%20Here%20is%20sample%20code%20to%20download%20and%20upload%20files%20to%20Azure%20Storage%20from%20Universal%20Windows%20Apps.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-287834%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Euniversal%20windows%20apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Sep 24, 2014

Here is sample code to download and upload files to Azure Storage from Universal Windows Apps.

Complete source code is available here

Introduction to Universal Windows Apps

Visual Studio template for Universal Windows Apps allows developers to build Windows Store App and Phone App in a single Visual Studio solution. This Visual Studio solution includes three projects, project for design and feature experiences unique to Store App, second project for Phone App and third project for shared code.  For more details on Universal Apps template in Visual Studio please click here

Here is the Visual Studio projects for Universal Windows Apps.

The project ending with .Windows is for Store App, project ending with .WindowsPhone is for Phone App and .Shared is for shared code.

In the Shared Project, use following #defines to write specific code for Phone App or Store App


#if WINDOWS_PHONE_APP
// For Windows Phone App
#elif WINDOWS_APP
// For Windows Store App
#else
// Error
#endif


Introduction to Azure Storage



Azure Storage is storage in cloud which is accessible from anywhere in the world, from any type of application, whether it’s running in the cloud, on the desktop, on an on-premises server, or on a mobile or tablet device. Azure Storage support four types of storage : Blob Storage, Table Storage, Queue Storage and File Storage. For more details please click here .



Steps to create Azure Storage







  1. Login to Azure Portal @ https://manage.windowsazure.com/


  2. Click on New at the bottom left


  3. Select Data Services | Storage | Quick Create and enter URL  as shown below











  4. Next create container, click on Create a Container in the Storage | UniversalAppAzureStorage | Container tab as shown below












  5. Enter a name for container as shown below












  6. Click on Manage Access Keys in the Dashboard tab and copy the Storage Account Name and Primary Access Key as shown













Steps to create Universal Windows App







  1. Open Visual Studio


  2. Click on File | New


  3. Select Templates | Visual C# | Store Apps | Universal Apps | Hub App as shown












  4. Now install NuGet Package for Azure Store


  5. Right click on the .Windows project and select Manage NuGet Packages


  6. In the search window type Azure Storage as shown below












  7. Click on Install button for Azure Storage


  8. Now install the Azure Storage NuGet Package on .WindowsPhone project


  9. Right click on the .WindowsPhone project and select Manage NuGet Packages


  10. In the search window type Azure Storage and click on the install button


  11. Now lets right code for uploading file to Azure Storage


  12. Open App.xaml.cs file in the .Shared Project


  13. Copy this below upload function

    private async Task<int> UploadToAzureStorage()
    {
    try
    {
    // create Azure Storage
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=universalappazurestorage;AccountKey=<your key>");

    // create a blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // create a container
    CloudBlobContainer container = blobClient.GetContainerReference("containerone");

    // create a block blob
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("filename");

    // create a local file
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename", CreationCollisionOption.ReplaceExisting);

    // copy some txt to local file
    MemoryStream ms = new MemoryStream();
    DataContractSerializer serializer = new DataContractSerializer(typeof(string));
    serializer.WriteObject(ms, "Hello Azure World!!");

    using (Stream fileStream = await file.OpenStreamForWriteAsync())
    {
    ms.Seek(0, SeekOrigin.Begin);
    await ms.CopyToAsync(fileStream);
    await fileStream.FlushAsync();
    }

    // upload to Azure Storage
    await blockBlob.UploadFromFileAsync(file);

    return 1;
    }
    catch
    {
    // return error
    return 0;
    }
    }



  14. Now copy this below download function

    private async Task<int> DownloadFromAzureStorage()
    {
    try
    {
    // create Azure Storage
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=universalappazurestorage;AccountKey=<your key>");

    // create a blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // create a container
    CloudBlobContainer container = blobClient.GetContainerReference("containerone");

    // create a block blob
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("filename");

    // create a local file
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename_from_azure", CreationCollisionOption.ReplaceExisting);

    // download from Azure Storage
    await blockBlob.DownloadToFileAsync(file);

    return 1;
    }
    catch
    {
    // return error
    return 0;
    }
    }



  15. Now call these two functions

    protected async override void OnLaunched(LaunchActivatedEventArgs e)
    {
    #if DEBUG
    if (System.Diagnostics.Debugger.IsAttached)
    {
    this.DebugSettings.EnableFrameRateCounter = true;
    }
    #endif
    int r = await UploadToAzureStorage();
    if (r == 1)
    {
    await DownloadFromAzureStorage();
    }

    Frame rootFrame = Window.Current.Content as Frame;



  16. Run the Windows Store App and check the files local storage


  17. To get the path of the local storage, look at the Path variable value of the StorageFile as shown












  18. here is the complete source code