locked
using hyperlink in gridview to download files in a folder from azure storage container RRS feed

  • Question

  • User1717218719 posted

    Hi all

    I am looking to download both image and text files from my azure storage container. the files are in a folder in my container and I am using a hyperlink in a gridview. I am coding in asp.net vb.

    I have the following code however it does not seem to be working I have used msgboxs and break points to try debug it but with no luck..

    any help would be great

    Thanks

    Partial Class DownloadFile
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    
            If Request.QueryString.Get("Id") IsNot Nothing Then
    
                Response.Clear()
    
                Dim id As Guid = New Guid(Request.QueryString.Get("Id"))
                '  Dim myFile As File = File.GetItem(id)
                MsgBox(id.ToString)
    
                        
                Dim account As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr)
                Dim client As CloudFileClient = account.CreateCloudFileClient()
                Dim cloudFileShare As CloudFileShare = client.GetShareReference("myfile")
                Dim root As CloudFileDirectory = cloudFileShare.GetRootDirectoryReference()
                Dim dir As CloudFileDirectory = root.GetDirectoryReference("myfolder")
                Dim file As CloudFile = dir.GetFileReference(id.ToString)
    
    '--This line below I think is not correct File.DownloadToFile("G:\test\" & id.ToString, System.IO.FileMode.OpenOrCreate) Response.Redirect("~/") End If End Sub End Class

    This is my asp.net code for my gridview:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="odsFiles" OnRowDataBound="GridView1_RowDataBound" >
            <Columns>
              <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
              <asp:BoundField DataField="OriginalName" HeaderText="OriginalName" SortExpression="OriginalName" />
              <asp:BoundField DataField="ContentType" HeaderText="ContentType" SortExpression="ContentType" />
              <asp:BoundField DataField="DateCreated" HeaderText="DateCreated" SortExpression="DateCreated" />
              <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="~/DownloadFile.aspx?Id={0}" HeaderText="Download" Text="Download" />
            </Columns>
          </asp:GridView>

    Monday, February 10, 2020 8:23 AM

Answers

  • User1717218719 posted

    I managed to get the code working !!

    If Request.QueryString.Get("Id") IsNot Nothing Then
    
                Response.Clear()
    
                Dim id As Guid = New Guid(Request.QueryString.Get("Id"))
    
               ' MsgBox(id.ToString)
    
    Dim path2 = "myfile\folder\" & id.ToString
                Dim BlobstorageConnection As String = CloudConfigurationManager.GetSetting(StorageConnStr)
                Dim MycloudStorageAccount As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr)
                Dim MyblobClient As CloudBlobClient = MycloudStorageAccount.CreateCloudBlobClient()
                Dim MycloudBlobContainer As CloudBlobContainer = MyblobClient.GetContainerReference(containerName)
    
    
                Dim MYblockBlob As CloudBlockBlob = MycloudBlobContainer.GetBlockBlobReference(path2 )
    
                Dim memStream As MemoryStream = New MemoryStream()
                MYblockBlob.DownloadToStream(memStream)
                HttpContext.Current.Response.ContentType = MYblockBlob.Properties.ContentType.ToString()
                HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" & MYblockBlob.ToString())
                HttpContext.Current.Response.AddHeader("Content-Length", MYblockBlob.Properties.Length.ToString())
                HttpContext.Current.Response.BinaryWrite(memStream.ToArray())
                HttpContext.Current.Response.Flush()
                HttpContext.Current.Response.Close()
    
                Response.Redirect("~/")
    
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 11, 2020 9:28 AM

All replies

  • User1717218719 posted

    Thanks Suresh,

    but I am looking to download my file directly from my azure storage container not from my sql database.

    Tuesday, February 11, 2020 7:37 AM
  • User1717218719 posted

    I have tried the below code which downloads the file perfectly if I input the file name however I cant seem to get it to work if I try to make it more generic.

    my file names are the same as the guid that I have stored in my gridview.

    any help would be great.

    Thanks

            If Request.QueryString.Get("Id") IsNot Nothing Then
    
                Response.Clear()
    
                Dim id As Guid = New Guid(Request.QueryString.Get("Id"))
    
               ' MsgBox(id.ToString)
    
    
                Dim BlobstorageConnection As String = CloudConfigurationManager.GetSetting(StorageConnStr)
                Dim MycloudStorageAccount As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr)
                Dim MyblobClient As CloudBlobClient = MycloudStorageAccount.CreateCloudBlobClient()
                Dim MycloudBlobContainer As CloudBlobContainer = MyblobClient.GetContainerReference(containerName)
    
                '  Dim MYblockBlob As CloudBlockBlob = MycloudBlobContainer.GetBlockBlobReference("testimg.jpg") '--WORKS
    
                Dim MYblockBlob As CloudBlockBlob = MycloudBlobContainer.GetBlockBlobReference(id.ToString)'--Not working
    
                Dim memStream As MemoryStream = New MemoryStream()
                MYblockBlob.DownloadToStream(memStream)
                HttpContext.Current.Response.ContentType = MYblockBlob.Properties.ContentType.ToString()
                HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" & MYblockBlob.ToString())
                HttpContext.Current.Response.AddHeader("Content-Length", MYblockBlob.Properties.Length.ToString())
                HttpContext.Current.Response.BinaryWrite(memStream.ToArray())
                HttpContext.Current.Response.Flush()
                HttpContext.Current.Response.Close()
    
                Response.Redirect("~/")
    
            End If

    Tuesday, February 11, 2020 9:05 AM
  • User1717218719 posted

    I managed to get the code working !!

    If Request.QueryString.Get("Id") IsNot Nothing Then
    
                Response.Clear()
    
                Dim id As Guid = New Guid(Request.QueryString.Get("Id"))
    
               ' MsgBox(id.ToString)
    
    Dim path2 = "myfile\folder\" & id.ToString
                Dim BlobstorageConnection As String = CloudConfigurationManager.GetSetting(StorageConnStr)
                Dim MycloudStorageAccount As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr)
                Dim MyblobClient As CloudBlobClient = MycloudStorageAccount.CreateCloudBlobClient()
                Dim MycloudBlobContainer As CloudBlobContainer = MyblobClient.GetContainerReference(containerName)
    
    
                Dim MYblockBlob As CloudBlockBlob = MycloudBlobContainer.GetBlockBlobReference(path2 )
    
                Dim memStream As MemoryStream = New MemoryStream()
                MYblockBlob.DownloadToStream(memStream)
                HttpContext.Current.Response.ContentType = MYblockBlob.Properties.ContentType.ToString()
                HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" & MYblockBlob.ToString())
                HttpContext.Current.Response.AddHeader("Content-Length", MYblockBlob.Properties.Length.ToString())
                HttpContext.Current.Response.BinaryWrite(memStream.ToArray())
                HttpContext.Current.Response.Flush()
                HttpContext.Current.Response.Close()
    
                Response.Redirect("~/")
    
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 11, 2020 9:28 AM