3

I have images in Azure that I need to add in a pdf using pdfJet.

This is the code that I use if I read an image on disk, however I have a lot of images and it does not make sense to download them from Azure.

Image image = new Image(objects, new BufferedStream(new FileStream(LocalPath + "image.PNG", FileMode.Open, FileAccess.Read)), ImageType.PNG);

PS: This is done in asp.net webforms.

Thank you for your help.

I am now using the following function to read a PDF:

    public MemoryStream DownloadToMemoryStream(DTO.BlobUpload b)
    {
        CloudStorageAccount storageAccount = Conn.SNString(b.OrgID);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(b.Container);
        CloudBlockBlob blob = container.GetBlockBlobReference(b.FileName);
        var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
        }, new SharedAccessBlobHeaders()
        {
            ContentDisposition = "attachment; filename=file-name"
        });
        using (MemoryStream ms = new MemoryStream())
        {
            blob.DownloadToStream(ms);
            return ms;
        }

    }

And in the aspx page I use:

MemoryStream pdfScript = B.DownloadToMemoryStream(b);

to read the stream:

SortedDictionary<Int32, PDFobj> objects = pdfFinalScript.Read(pdfScript);

However I get the error message: Cannot access a closed Stream

I have looked at how to open the stream but haven't managed to do it.

Could you please help, thank you

Anne
  • 91
  • 2
  • 13

1 Answers1

1

According to your description, you would download blobs from Azure. Here are several ways you could refer to.

1.Download with blob url.

Create a Shared Access Signature with Read permission and Content-Disposition header set and create blob URL based on that and use that URL. In this case, the blob contents will be directly streamed from storage to the client browser.

2.Get the blob and DownloadFileFromBlob.

3.Download file to exactly path in local.

Webform:

You could use Response.Redirect(blobUrl); to redirect blob url and download it.

In .aspx:

<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" /> 

In aspx.cs:

protected void Button1_Click(object sender, EventArgs e)
        {
            CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
            var blobClient = account.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference("container");
            var blob = container.GetBlockBlobReference("text.PNG");
            var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                Permissions = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            }, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=file-name"
            });
            using (MemoryStream ms = new MemoryStream())
            {
                blob.DownloadToStream(ms);
                Image image = new Image(objects, ms, ImageType.PNG);
            }
        }
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Hi, it looks like option 1 would be the one to go, however, I am using asp.net webforms, and I am not sure how to return the url using webforms. Any idea? thank you – Anne Aug 21 '18 at 21:25
  • @Anne, I have update my reply, you could refer to it. If you have any problem, please feel free to let me know. – Joey Cai Aug 22 '18 at 07:06
  • Hi Joey, I am still a bit confused on how to use this response.redirect, as at the end I need to file stream the blob this way: Image image = new Image(objects, new BufferedStream(new FileStream( blobUrl, FileMode.Open, FileAccess.Read)), ImageType.PNG); – Anne Aug 23 '18 at 12:15
  • @Anne, you could download blob to Stream, then you do not need to translate blob to stream. – Joey Cai Aug 24 '18 at 05:56
  • Hi Joey, Sorry it took me a while to respond, I am almost there I think, I had to modify your function as I want to use it to open a PDF in Azure, I have modify my question to reflect what I am looking for. Could you please have a look. Thank you – Anne Aug 30 '18 at 08:57
  • Yes, then you could directly use the Stream in Image Method. So, any problem you have now? – Joey Cai Aug 30 '18 at 08:58
  • Yes, but in the example I added, the function is returning a memory stream: MemoryStream pdfScript = B.DownloadToMemoryStream(b); and when I read the stream: SortedDictionary objects = pdfFinalScript.Read(pdfScript); I get an error message; Cannot access a close stream. I have updated my question with more detail. I cannot figure out how to open the stream. – Anne Aug 30 '18 at 11:25