Problem downloading a PDF file from Azure File to bytes using Azure.Storage.Files.Shares

1.1k views Asked by At

I previously used Windows.AzureStorage to download PDF files from my Azure storage account and stream the PDF to a browser. Since it was deprecated, I am trying to get in line with Azure.Files.Storage.Shares.

Note that I was able to use Windows.AzureStorage for years.

I am able to get the file from Azure and read the bytes but when I try to stream them to the browser the file is not a valid PDF.

The old way of doing it looked like this:

                    pdfFile.FetchAttributes()

                    Dim byteData As Byte() = New Byte(pdfFile.Properties.Length - 1) {}

                    pdfFile.DownloadToByteArray(byteData, 0)

                    byteReturnValue = byteData

This is a hard coded test function that "works" but does not render a valid PDF. The number of bytes returned using the new method shown below is 98,284. The number of bytes returned in the previous (successful) method is 98,284. Is it something to do with the "Offset"??

I'm at my wits end.

    Public Shared Function GetPDFStream() As Byte()

    Dim connectionString As String = ConfigurationManager.AppSettings("StorageConnectionString")
    Dim shareName As String = "root"
    Dim dirName As String = "invoicestest\MSEVN"
    Dim fileName As String = "EVE0042361.pdf"
    Dim share As ShareClient = New ShareClient(connectionString, shareName)
    Dim directory As ShareDirectoryClient
    Dim cloudfile As ShareFileClient
    Dim download As ShareFileDownloadInfo

    directory = share.GetDirectoryClient(dirName)

    cloudfile = directory.GetFileClient(fileName)

    download = cloudfile.Download()

    Dim byteData As Byte() = New Byte(download.ContentLength - 1) {}

    download.Content.Read(byteData, 0, download.ContentLength - 1)

    GetPDFStream = byteData

End Function

These bytes get returned to another page which outputs the bytes to the browser (this part is fine but I am showing how the bytes get used)

                    Response.Buffer = True

                    Dim strMimeType = "application/pdf"

                    Response.Clear()
                    Response.ContentType = strMimeType

                    Response.AddHeader("content-disposition", "attachment;filename=" & strFileName)

                    Response.OutputStream.Write(byteReturnValue, 0, byteReturnValue.Length)
                    Response.OutputStream.Flush()
                    Response.OutputStream.Close()
                    Response.Flush()
                    Response.Close()

1

There are 1 answers

0
Joe Schmucker On

Found a solution. This is the code that works. Much simpler too. I got the idea from this question Thank you Jon Skeet!!

Public Shared Function GetPDFStream() As Byte()

    Dim connectionString As String = 
    ConfigurationManager.AppSettings("StorageConnectionString")
    Dim shareName As String = "root"
    Dim dirName As String = "invoicestest\MSEVN"
    Dim fileName As String = "EVE0042361.pdf"
    Dim share As ShareClient = New ShareClient(connectionString, shareName)
    Dim directory As ShareDirectoryClient
    Dim cloudfile As ShareFileClient
    Dim download As ShareFileDownloadInfo

    directory = share.GetDirectoryClient(dirName)

    cloudfile = directory.GetFileClient(fileName)

    download = cloudfile.Download()

    Dim stream As New MemoryStream

    download.Content.CopyTo(stream)

    GetPDFStream = stream.ToArray()

End Function