Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Blob Storage - Using filestream/memorystream to display PDF

I've set up an Azure Blob Storage to upload/download file.

ProcessAsync(Folderpath, rows.Item("FileName"), rows.Item("Extension")).GetAwaiter().GetResult()
System.IO.File.Move(Folderpath + "\" + rows.Item("FileName"), AchivePath + "\" + rows.Item("FileName"))
WriteToLog("Extracted File : " + rows.Item("FileName") + " ", "")
DownloadBlobSnapshot(rows.Item("FileName"), rows.Item("Extension")).GetAwaiter().GetResult()

Currently, I am able to store my file locally in my desktop. How do I save and display the file on .net MVC platform with Using and MemoryStream/FileStream?

Or alternatively, is there any better way in doing so?

Edit 1: (Based on Ivan's Answer)

Private Shared Async Function DownloadBlobSnapshot(FileName As String, Extension As String) As Task

Dim storageAccount As CloudStorageAccount
Dim storageConnectionString As String = ConfigurationManager.AppSettings("StorageConnectionString")
Dim accountName As String = myaccountname
Dim accountKey As String = myaccountkey
Dim cred = New StorageCredentials(accountName, accountKey)
Dim account = New CloudStorageAccount(cred, True)
Dim client = account.CreateCloudBlobClient()
Dim container = client.GetContainerReference(FileName.Replace(Extension, ""))
Dim listOfBlobs As IEnumerable = container.ListBlobs(Nothing, True, BlobListingDetails.Snapshots)

If CloudStorageAccount.TryParse(storageConnectionString, storageAccount) Then
   For Each item In listOfBlobs
       Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference((CType(item, CloudBlockBlob)).Name)
       blockBlob.DownloadToStream(Response.OutputStream)
   Next
       //ToDO
        End If
End Function

But I'd realised Response is not available in Shared Function.

like image 789
PartTimeNerd Avatar asked Dec 23 '22 02:12

PartTimeNerd


1 Answers

You can use DownloadToStream.

Sample code as below(asp.net mvc project):

        public ActionResult DownloadFile()
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your_account", "your_key"),true);
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference("t11");
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference("ss22.pdf");

            blob.DownloadToStream(Response.OutputStream);

            return new EmptyResult();
        }

Update 1: upload the screenshot of vb.net mvc project: enter image description here

like image 61
Ivan Yang Avatar answered Dec 29 '22 14:12

Ivan Yang