• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2290
  • Last Modified:

Adobe Reader Error: The File is damaged and could not be repaired...

We need to open PDF files without showing the URL or downloading the file to the 'downloads' directory.

So we found this but of code to help us do this:
http://stackoverflow.com/questions/657591/delete-dynamically-generated-pdf-file-immediately-after-it-has-been-displayed-to

Code to download file (downloadodcs.aspx):
==========================
    Response.Buffer = false;
    Response.BufferOutput = false;  
    Response.ContentType = "application/pdf";

    Stream outstream = Response.OutputStream;
    FileStream instream =
        new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

    byte[] buffer = new byte[10000];
    int len;
    while ((len = instream.Read(buffer, 0, 10000)) > 0)
    {
        outstream.Write(buffer, 0, len);
    }
    outstream.Flush();
    instream.Close();

    // served the file -> now delete it
    File.Delete(path);
====================================

But once it opens one or two PDF's it display's this error in a pop-up box when trying to open another document:
The File is damaged and could not be repaired...

It seems to be filling up the input buffer. And we have tried changing the size of the buffer but it keeps filling up. What am I doing wrong?
0
dcrowley_01
Asked:
dcrowley_01
1 Solution
 
apeterCommented:
Shouldn't your below buffer size should be equal to your file size ? Instead of hardcoding the 10000,  dynamically calculate the file size which is streamed and create the buffer.

byte[] buffer = new byte[10000];
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now