Weird C# File to byte array issue RRS feed

  • Question

  • Hey everyone!

    I have a weird problem. I am making a C# MVC application which generates PDF's and offers them for download with a download button.

            public ActionResult Download()
                string url = (string)TempData["url"];
                byte[] thePdf = System.IO.File.ReadAllBytes(url);
                return File(thePdf, "application/pdf");

    All of a sudden I can't properly convert a PDF file to byte[], either with File.ReadAllBytes() or with a Memorystream (or any other stream).

    When I used a Memorystream I got an InvalidOperationException on both the ReadTimeOut and WriteTimeOut.

    I implemented the code mentioned above in a new C# MVC Project and there everything worked fine. So the issue must be with the project that i'm working in.


    When I read out the bytes, it does return a full byte array but when it is downloaded and converted back to a PDF, the PDF is empty (all pages do exist) and the name is some collection of weird characters.

    In the new project, it is just the normal PDF as it should be.

    Does anyone have any clue what this might be and how i could fix it?

    • Moved by CoolDadTx Friday, April 5, 2019 1:51 PM ASP.NET related
    Friday, April 5, 2019 12:52 PM

All replies

  • Please post your question in normal font and use the Insert Code Block to put your code into a code block so we can more easily read it.

    For questions related to MVC please post in the MVC forums. Since you mentioned the downloaded file is empty this sounds like an MVC-related issue.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 5, 2019 1:51 PM