views:

57

answers:

3

I have an ASP.NET MVC2 application in development and I am having problems rendering a .pdf file from our production server.

On my Visual Studio 2010 integrated development server everything works fine, but after I publish the application to the production server, it breaks. It does not throw any exceptions or errors of any kind, it simply does not show the file.

Here's my function for displaying the PDF document:

public static void PrintExt(byte[] FileToShow, String TempFileName, 
                                                       String Extension)
{
    String ReportPath = Path.GetTempFileName() + '.' + Extension;

    BinaryWriter bwriter = 
        new BinaryWriter(System.IO.File.Open(ReportPath, FileMode.Create));
    bwriter.Write(FileToShow);
    bwriter.Close();

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = ReportPath;
    p.StartInfo.UseShellExecute = true;
    p.Start();
}

My production server is running Windows Server 2008 and IIS 7.

+2  A: 

i suggest you use ASP.NET MVC FileResult Class to display the PDF.

see http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx

your code open`s the PDF on the webserver.

marc.d
+4  A: 

You cannot expect opening the default program associated with PDF file browsing on the server. Try returning the file into the response stream which will effectively open it on the client machine:

public ActionResult ShowPdf()
{
    byte[] fileToShow = FetchPdfFile();
    return File(fileToShow, "application/pdf", "report.pdf");
}

And now navigate to /somecontroller/showPdf. If you want the PDF opening inside the browser instead of showing the download dialog you may try adding the following to the controller action before returning:

Response.AddHeader("Content-Disposition", "attachment; filename=report.pdf");
Darin Dimitrov
A: 

Here's how I did it.

public ActionResult PrintPDF(byte[] FileToShow, String TempFileName, String Extension)
    {
        String ReportPath = Path.GetTempFileName() + '.' + Extension;

        BinaryWriter bwriter = new BinaryWriter(System.IO.File.Open(ReportPath, FileMode.Create));
        bwriter.Write(FileToShow);
        bwriter.Close();

        return base.File(FileToShow, "application/pdf");
    }

Thank you all for your efforts. Solution I used is the most similar to the Darin's one (almost the same, but his is prettier :D), so I will accept his solution.

Vote up for all of you folks (both for answers and comments)

Thanks

Eedoh