'Error Failed to Load PDF Document' when Returning PDF From Azure Blob StorageError while opening PDFOpen a pdf file(using bytes) stored in Azure Blob storageChecking if a blob exists in Azure StorageAzure Blob file storage strategyAzure Blob always downloads when navigating to urlStoring Excel/Word documents Windows Azure Blob Storage Files CorruptedAzure blob storage returns 404 with some filesSaving a MemoryStream to Azure blob storageDownloading PDF from azure fails to load when reopeningDownload multiple blob files as a zip from azure storageOpen a pdf file(using bytes) stored in Azure Blob storageOpening PDF from Blob Storage URL in new tab C# MVC

What language is Raven using for her attack in the new 52?

Copying an existing HTML page and use it, is that against any copyright law?

Why/when is AC-DC-AC conversion superior to direct AC-AC conversion?

Decreasing star count

Correlation length anisotropy in the 2D Ising model

Pointwise convergence of uniformly continuous functions to zero, but not uniformly

Did the IBM PC use the 8088's NMI line?

How did the Axis intend to hold the Caucasus?

Heisenberg uncertainty principle in daily life

Isolated audio without a transformer

Commercial jet accompanied by small plane near Seattle

Assuring luggage isn't lost with short layover

May a man marry the women with whom he committed adultery?

Checking if an integer is a member of an integer list

Which approach can I use to generate text based on multiple inputs?

What does "see" in "the Holy See" mean?

Why did House of Representatives need to condemn Trumps Tweets?

How to tar a list of directories only if they exist

Is a topological space considered to be a class in set theory?

Why can't my huge trees be chopped down?

Why force the nose of 737 Max down in the first place?

Seaborn style plot of pandas dataframe

Trapped in an ocean Temple in Minecraft?

Why do planes need a roll motion?



'Error Failed to Load PDF Document' when Returning PDF From Azure Blob Storage


Error while opening PDFOpen a pdf file(using bytes) stored in Azure Blob storageChecking if a blob exists in Azure StorageAzure Blob file storage strategyAzure Blob always downloads when navigating to urlStoring Excel/Word documents Windows Azure Blob Storage Files CorruptedAzure blob storage returns 404 with some filesSaving a MemoryStream to Azure blob storageDownloading PDF from azure fails to load when reopeningDownload multiple blob files as a zip from azure storageOpen a pdf file(using bytes) stored in Azure Blob storageOpening PDF from Blob Storage URL in new tab C# MVC






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I am trying to get my MVC controller to return a PDF stored in an Azure Blob Container. The client's browser will download the PDF fine, but when they open it they will see "Error Failed to load PDF document." when opening it in Chrome (though the file does not open in other browsers either).



I was able to download the file on my machine and open it fine doing the following:



public static void DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");

AsyncCallback callback = new AsyncCallback(DownloadComplete);
cloudBlockBlob.BeginDownloadToFile(@"pathdocument.pdf", FileMode.Create,
callback, new object());



However I would rather not create any temp files on the server and return those. I would like to just create them in memory and return that to the client.



My Controller Code:



public async Task<FileStreamResult> Test()

MemoryStream stream = await BlobStorageUtils.DownloadFile();
return File(stream, "application/pdf", "document.pdf");



The code to retrieve the file from the Blob Container



public static async Task<MemoryStream> DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");
MemoryStream stream = new MemoryStream();
await cloudBlockBlob.DownloadToStreamAsync(stream);
return stream;



As I mentioned, my file downloads in the browser fine, but I receive the error when trying to open the file.



Eventually I would like this to work with any type of document, not just PDFs.



Edit: I should note that I have tried this with image files as well (PNG specifically) and had a similar issue where the image was corrupted or couldn't be opened. The error I received then was "It looks like we don't support this file format".



Update: See my solution below for how I got this to work.










share|improve this question
























  • Maybe a rather stupid question, but is the default PDF viewer of your browser(is) correct? Are you able to open other PDFs in the non-Chrome browsers? Perhaps the error-while-opening-pdf page contains the answer?

    – Odrai
    Mar 26 at 18:53












  • @Odrai Unfortunately I wish I were that simple. I tried opening the PDF in multiple different browsers / applications and none of them work. It is almost like the PDF is corrupted somehow.

    – deruitda
    Mar 26 at 19:08






  • 1





    Did you check the next Q/A as well: link? :)

    – Odrai
    Mar 26 at 19:11











  • Perfect!! Thank you @Odrai, I will update my post with the fix for this.

    – deruitda
    Mar 26 at 19:25











  • You are more than welcome, I'm glad I was able to help. Leave you question as is and add a 'Update 1' paragraph, so other StackOverflow users are able to view the changes you made :)

    – Odrai
    Mar 26 at 19:29


















2















I am trying to get my MVC controller to return a PDF stored in an Azure Blob Container. The client's browser will download the PDF fine, but when they open it they will see "Error Failed to load PDF document." when opening it in Chrome (though the file does not open in other browsers either).



I was able to download the file on my machine and open it fine doing the following:



public static void DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");

AsyncCallback callback = new AsyncCallback(DownloadComplete);
cloudBlockBlob.BeginDownloadToFile(@"pathdocument.pdf", FileMode.Create,
callback, new object());



However I would rather not create any temp files on the server and return those. I would like to just create them in memory and return that to the client.



My Controller Code:



public async Task<FileStreamResult> Test()

MemoryStream stream = await BlobStorageUtils.DownloadFile();
return File(stream, "application/pdf", "document.pdf");



The code to retrieve the file from the Blob Container



public static async Task<MemoryStream> DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");
MemoryStream stream = new MemoryStream();
await cloudBlockBlob.DownloadToStreamAsync(stream);
return stream;



As I mentioned, my file downloads in the browser fine, but I receive the error when trying to open the file.



Eventually I would like this to work with any type of document, not just PDFs.



Edit: I should note that I have tried this with image files as well (PNG specifically) and had a similar issue where the image was corrupted or couldn't be opened. The error I received then was "It looks like we don't support this file format".



Update: See my solution below for how I got this to work.










share|improve this question
























  • Maybe a rather stupid question, but is the default PDF viewer of your browser(is) correct? Are you able to open other PDFs in the non-Chrome browsers? Perhaps the error-while-opening-pdf page contains the answer?

    – Odrai
    Mar 26 at 18:53












  • @Odrai Unfortunately I wish I were that simple. I tried opening the PDF in multiple different browsers / applications and none of them work. It is almost like the PDF is corrupted somehow.

    – deruitda
    Mar 26 at 19:08






  • 1





    Did you check the next Q/A as well: link? :)

    – Odrai
    Mar 26 at 19:11











  • Perfect!! Thank you @Odrai, I will update my post with the fix for this.

    – deruitda
    Mar 26 at 19:25











  • You are more than welcome, I'm glad I was able to help. Leave you question as is and add a 'Update 1' paragraph, so other StackOverflow users are able to view the changes you made :)

    – Odrai
    Mar 26 at 19:29














2












2








2








I am trying to get my MVC controller to return a PDF stored in an Azure Blob Container. The client's browser will download the PDF fine, but when they open it they will see "Error Failed to load PDF document." when opening it in Chrome (though the file does not open in other browsers either).



I was able to download the file on my machine and open it fine doing the following:



public static void DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");

AsyncCallback callback = new AsyncCallback(DownloadComplete);
cloudBlockBlob.BeginDownloadToFile(@"pathdocument.pdf", FileMode.Create,
callback, new object());



However I would rather not create any temp files on the server and return those. I would like to just create them in memory and return that to the client.



My Controller Code:



public async Task<FileStreamResult> Test()

MemoryStream stream = await BlobStorageUtils.DownloadFile();
return File(stream, "application/pdf", "document.pdf");



The code to retrieve the file from the Blob Container



public static async Task<MemoryStream> DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");
MemoryStream stream = new MemoryStream();
await cloudBlockBlob.DownloadToStreamAsync(stream);
return stream;



As I mentioned, my file downloads in the browser fine, but I receive the error when trying to open the file.



Eventually I would like this to work with any type of document, not just PDFs.



Edit: I should note that I have tried this with image files as well (PNG specifically) and had a similar issue where the image was corrupted or couldn't be opened. The error I received then was "It looks like we don't support this file format".



Update: See my solution below for how I got this to work.










share|improve this question
















I am trying to get my MVC controller to return a PDF stored in an Azure Blob Container. The client's browser will download the PDF fine, but when they open it they will see "Error Failed to load PDF document." when opening it in Chrome (though the file does not open in other browsers either).



I was able to download the file on my machine and open it fine doing the following:



public static void DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");

AsyncCallback callback = new AsyncCallback(DownloadComplete);
cloudBlockBlob.BeginDownloadToFile(@"pathdocument.pdf", FileMode.Create,
callback, new object());



However I would rather not create any temp files on the server and return those. I would like to just create them in memory and return that to the client.



My Controller Code:



public async Task<FileStreamResult> Test()

MemoryStream stream = await BlobStorageUtils.DownloadFile();
return File(stream, "application/pdf", "document.pdf");



The code to retrieve the file from the Blob Container



public static async Task<MemoryStream> DownloadFile()

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");
MemoryStream stream = new MemoryStream();
await cloudBlockBlob.DownloadToStreamAsync(stream);
return stream;



As I mentioned, my file downloads in the browser fine, but I receive the error when trying to open the file.



Eventually I would like this to work with any type of document, not just PDFs.



Edit: I should note that I have tried this with image files as well (PNG specifically) and had a similar issue where the image was corrupted or couldn't be opened. The error I received then was "It looks like we don't support this file format".



Update: See my solution below for how I got this to work.







c# asp.net-mvc azure azure-blob-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 19:31







deruitda

















asked Mar 26 at 18:44









deruitdaderuitda

888 bronze badges




888 bronze badges












  • Maybe a rather stupid question, but is the default PDF viewer of your browser(is) correct? Are you able to open other PDFs in the non-Chrome browsers? Perhaps the error-while-opening-pdf page contains the answer?

    – Odrai
    Mar 26 at 18:53












  • @Odrai Unfortunately I wish I were that simple. I tried opening the PDF in multiple different browsers / applications and none of them work. It is almost like the PDF is corrupted somehow.

    – deruitda
    Mar 26 at 19:08






  • 1





    Did you check the next Q/A as well: link? :)

    – Odrai
    Mar 26 at 19:11











  • Perfect!! Thank you @Odrai, I will update my post with the fix for this.

    – deruitda
    Mar 26 at 19:25











  • You are more than welcome, I'm glad I was able to help. Leave you question as is and add a 'Update 1' paragraph, so other StackOverflow users are able to view the changes you made :)

    – Odrai
    Mar 26 at 19:29


















  • Maybe a rather stupid question, but is the default PDF viewer of your browser(is) correct? Are you able to open other PDFs in the non-Chrome browsers? Perhaps the error-while-opening-pdf page contains the answer?

    – Odrai
    Mar 26 at 18:53












  • @Odrai Unfortunately I wish I were that simple. I tried opening the PDF in multiple different browsers / applications and none of them work. It is almost like the PDF is corrupted somehow.

    – deruitda
    Mar 26 at 19:08






  • 1





    Did you check the next Q/A as well: link? :)

    – Odrai
    Mar 26 at 19:11











  • Perfect!! Thank you @Odrai, I will update my post with the fix for this.

    – deruitda
    Mar 26 at 19:25











  • You are more than welcome, I'm glad I was able to help. Leave you question as is and add a 'Update 1' paragraph, so other StackOverflow users are able to view the changes you made :)

    – Odrai
    Mar 26 at 19:29

















Maybe a rather stupid question, but is the default PDF viewer of your browser(is) correct? Are you able to open other PDFs in the non-Chrome browsers? Perhaps the error-while-opening-pdf page contains the answer?

– Odrai
Mar 26 at 18:53






Maybe a rather stupid question, but is the default PDF viewer of your browser(is) correct? Are you able to open other PDFs in the non-Chrome browsers? Perhaps the error-while-opening-pdf page contains the answer?

– Odrai
Mar 26 at 18:53














@Odrai Unfortunately I wish I were that simple. I tried opening the PDF in multiple different browsers / applications and none of them work. It is almost like the PDF is corrupted somehow.

– deruitda
Mar 26 at 19:08





@Odrai Unfortunately I wish I were that simple. I tried opening the PDF in multiple different browsers / applications and none of them work. It is almost like the PDF is corrupted somehow.

– deruitda
Mar 26 at 19:08




1




1





Did you check the next Q/A as well: link? :)

– Odrai
Mar 26 at 19:11





Did you check the next Q/A as well: link? :)

– Odrai
Mar 26 at 19:11













Perfect!! Thank you @Odrai, I will update my post with the fix for this.

– deruitda
Mar 26 at 19:25





Perfect!! Thank you @Odrai, I will update my post with the fix for this.

– deruitda
Mar 26 at 19:25













You are more than welcome, I'm glad I was able to help. Leave you question as is and add a 'Update 1' paragraph, so other StackOverflow users are able to view the changes you made :)

– Odrai
Mar 26 at 19:29






You are more than welcome, I'm glad I was able to help. Leave you question as is and add a 'Update 1' paragraph, so other StackOverflow users are able to view the changes you made :)

– Odrai
Mar 26 at 19:29













1 Answer
1






active

oldest

votes


















1














The solution for this came from this link: Open a pdf file(using bytes) stored in Azure Blob storage



I ended up just dumping the byte stream from Azure into the response's output stream. However you need to make sure that the content type of the response is set to "application/pdf". My code ended up like this:



Controller Code:



public async Task<ActionResult> Test()

Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";

await BlobStorageUtils.DownloadFile(Response.OutputStream);

return new EmptyResult();



Code to retrieve file from Blob Container



public static async Task DownloadFile(Stream outputStream)

CloudBlockBlob cloudBlockBlob =
CloudBlobContainer.GetBlockBlobReference("document.pdf");
await cloudBlockBlob.DownloadToStreamAsync(outputStream);






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364244%2ferror-failed-to-load-pdf-document-when-returning-pdf-from-azure-blob-storage%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The solution for this came from this link: Open a pdf file(using bytes) stored in Azure Blob storage



    I ended up just dumping the byte stream from Azure into the response's output stream. However you need to make sure that the content type of the response is set to "application/pdf". My code ended up like this:



    Controller Code:



    public async Task<ActionResult> Test()

    Response.Buffer = true;
    Response.Clear();
    Response.ContentType = "application/pdf";

    await BlobStorageUtils.DownloadFile(Response.OutputStream);

    return new EmptyResult();



    Code to retrieve file from Blob Container



    public static async Task DownloadFile(Stream outputStream)

    CloudBlockBlob cloudBlockBlob =
    CloudBlobContainer.GetBlockBlobReference("document.pdf");
    await cloudBlockBlob.DownloadToStreamAsync(outputStream);






    share|improve this answer





























      1














      The solution for this came from this link: Open a pdf file(using bytes) stored in Azure Blob storage



      I ended up just dumping the byte stream from Azure into the response's output stream. However you need to make sure that the content type of the response is set to "application/pdf". My code ended up like this:



      Controller Code:



      public async Task<ActionResult> Test()

      Response.Buffer = true;
      Response.Clear();
      Response.ContentType = "application/pdf";

      await BlobStorageUtils.DownloadFile(Response.OutputStream);

      return new EmptyResult();



      Code to retrieve file from Blob Container



      public static async Task DownloadFile(Stream outputStream)

      CloudBlockBlob cloudBlockBlob =
      CloudBlobContainer.GetBlockBlobReference("document.pdf");
      await cloudBlockBlob.DownloadToStreamAsync(outputStream);






      share|improve this answer



























        1












        1








        1







        The solution for this came from this link: Open a pdf file(using bytes) stored in Azure Blob storage



        I ended up just dumping the byte stream from Azure into the response's output stream. However you need to make sure that the content type of the response is set to "application/pdf". My code ended up like this:



        Controller Code:



        public async Task<ActionResult> Test()

        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "application/pdf";

        await BlobStorageUtils.DownloadFile(Response.OutputStream);

        return new EmptyResult();



        Code to retrieve file from Blob Container



        public static async Task DownloadFile(Stream outputStream)

        CloudBlockBlob cloudBlockBlob =
        CloudBlobContainer.GetBlockBlobReference("document.pdf");
        await cloudBlockBlob.DownloadToStreamAsync(outputStream);






        share|improve this answer















        The solution for this came from this link: Open a pdf file(using bytes) stored in Azure Blob storage



        I ended up just dumping the byte stream from Azure into the response's output stream. However you need to make sure that the content type of the response is set to "application/pdf". My code ended up like this:



        Controller Code:



        public async Task<ActionResult> Test()

        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "application/pdf";

        await BlobStorageUtils.DownloadFile(Response.OutputStream);

        return new EmptyResult();



        Code to retrieve file from Blob Container



        public static async Task DownloadFile(Stream outputStream)

        CloudBlockBlob cloudBlockBlob =
        CloudBlobContainer.GetBlockBlobReference("document.pdf");
        await cloudBlockBlob.DownloadToStreamAsync(outputStream);







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 26 at 19:37

























        answered Mar 26 at 19:30









        deruitdaderuitda

        888 bronze badges




        888 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364244%2ferror-failed-to-load-pdf-document-when-returning-pdf-from-azure-blob-storage%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript