Is there a way to view PDF files in Blob Storage without downloading them locally using .net?

Murugan M 31 Reputation points
2022-05-26T14:38:59.717+00:00

Using a Blob URL I can't open a PDF file in chrome. Whenever I used to open the PDF file using that blob URL its getting downloaded! Instead for View!

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,200 questions
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Murugan M 31 Reputation points
    2022-05-27T04:43:51.337+00:00

    BlobContainerClient container = new BlobContainerClient("DefaultEndpointsProtocol=https;AccountName={{AzureAccountName}};AccountKey={{key}};EndpointSuffix={{endPoint}}", {{containernName}});
    DateTime fileCreationDatetime = DateTime.Now;
    string PDFName = fileCreationDatetime.ToString("yyyyMMddHHMMss") + '_' + ".pdf";
    base64String = base64String.Substring(base64String.IndexOf(',') + 1);
    byte[] imageBytes = Convert.FromBase64String(base64String);
    Stream stream = new MemoryStream(imageBytes);
    BlobClient blobClient = container.GetBlobClient(PDFName);
    blobClient.Upload(stream, true);
    PDFBlobPath = blobClient.Uri.AbsoluteUri;
    BlobHttpHeaders blobHeaders = new BlobHttpHeaders();
    blobHeaders.ContentType = "application/pdf";
    blobClient.SetHttpHeaders(blobHeaders);

    This will works if you all the namesoace and refrences correctly

    5 people found this answer helpful.

  2. Michael Taylor 60,326 Reputation points
    2022-05-26T15:59:09.903+00:00

    Browsers control what happens when a URL points to a file. All browsers that I'm aware of today download the file and that's it. But it is up to the browser and the user's configuration. Your server app has no control over this. You cannot view a file from the browser without downloading irrelevant.

    For Edge and PDFs specifically you can go to Settings\Cookies and Site permissions\PDF Documents and check/uncheck the Always download PDF files option. This controls whether Edge tries to open the PDF after downloading.

    For Chrome and PDFs go to Privacy and security\Site Settinogs\Content\Additional Content settings\PDF documents and you can do the same thing.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.