0

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.

EDIT:

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? (I can clarify more if needed)

  • 1
    what's in `url`? – Daniel A. White Apr 05 '19 at 13:05
  • url is something similar to this:"D:\\folder\\folder\\folder\\folder\\folder\\PDFs\\4ef656d7aa4d8a7c82c37bb629396fe2e2638e345235fd72.pdf" But the main note is, this all used to work perfectly, all of a sudden it stopped working even tho no changes were made (as far as i Know) – Sander Berntsen Apr 05 '19 at 13:06
  • I would use streams instead of reading the whole pdf in memory. – Serban Murariu Apr 05 '19 at 13:09
  • well I tried with both File.ReadAllBytes and using a memory stream in the sense of `using (Stream fs = new FileStream(outputPdfFilePath, FileMode.Open)) { byte[] buffer = new byte[32 * 1024]; int read; while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) woutput.OutputStream.Write(buffer, 0, read); }` but they all returned a full byte array wiith values, but once converted back to PDF via download button in MVC, I get a blank PDF. – Sander Berntsen Apr 05 '19 at 13:11
  • Have a look at this answer => https://stackoverflow.com/questions/38036534/mvc-how-to-request-and-save-a-pdf-file-from-a-url – Dimitri Apr 05 '19 at 13:17
  • Why are you even looking at `Read`/`WriteTimeout`? By default, *all streams* throw on accessing them and `MemoryStream` doesn't override this. timeouts on a memory stream *don't make sense*. – Damien_The_Unbeliever Apr 05 '19 at 13:27
  • I actually didn't know that, I'm sorry. I just mistook it for the error since it didn't work and I really couldn't find the cause. – Sander Berntsen Apr 05 '19 at 13:35

2 Answers2

0

Try something like this:

public ActionResult Download()
{
    string url = (string)TempData["url"];

    using (WebClient client = new WebClient())
    {
       // Download data.
       byte[] thePdf = client.DownloadData("http://url-to-your-pdf-file.com/file1");
       return File(thePdf,"application/pdf");
    }
}
Dimitri
  • 1,185
  • 2
  • 15
  • 37
  • With this I just get an error that it cannot open the file. so I don't think this solution if fully compatible with MVC (had to change it to return File(thePdf, path) aswell) – Sander Berntsen Apr 05 '19 at 13:27
0

Seems like I had a line or two too much in FilterConfig. I added 2 custom filters and that seemed to mess up everything. Now that i removed them everything works again!