使用asp.net C#打开PDF文件时出现问题 [英] Problem opening a pdf file with asp.net c#

查看:72
本文介绍了使用asp.net C#打开PDF文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我.我正在尝试使用c#在asp.net应用程序中打开pdf文件.当我在装有Visual Studio的PC上运行它时,它可以正常工作.当我尝试在服务器上运行它时,该文档没有打开.如果我查看任务管理器,该过程确实会启动,并且我的网页会闪烁,好像正在刷新,但pdf文档无法打开.我尝试了两种不同的方法,但均无效.

I hope someone can help me with this. I am trying to open a pdf file in my asp.net application using c#. When I run it on my PC with Visual Studio, it works fine. When I try to run it on the server, the document doesn''t open. If I look at the Task Manager, the process does start and my web page flickers as if it is refreshing but the pdf document does not open up. I have tried two different methods and neither works.

string FilePath = Request.PhysicalApplicationPath.ToString() + "Pages\\UserGuide\\UserGuide.pdf";
       ProcessStartInfo startInfo = new ProcessStartInfo(FilePath);
       Process.Start(startInfo);







and

string FilePath = Request.PhysicalApplicationPath.ToString() + "Pages\\UserGuide\\UserGuide.pdf";
        System.Diagnostics.Process Proc = new System.Diagnostics.Process();
        Proc.EnableRaisingEvents = true;
        Proc.StartInfo.FileName = FilePath;
        Proc.StartInfo.Arguments = "AcroRd32.exe";
        Proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
        Proc.StartInfo.CreateNoWindow = false;
        Proc.Start();



另外,如何以及何时取消该进程?

预先感谢您的任何建议.



Also, how and when do I kill the process?

Thanks in advance for any suggestions.

推荐答案

Remove the "attachment; " portion of the "Content-Disposition" header and it should work.  You should also send the proper mime type (text/html for HTML documents, for instance), but it is not always necessary and application/octet-stream basically lets the browser figure it out.  Testing on my own machine with various file types, it opened up the new window and either loaded the file in the window (text, HTML), or required me to open it with the Download dialog, which closed the window when I downloaded the item.  One case did not close the window, which was a PDF file using "application/octet-stream" but by providing "application/pdf" it was correctly opened in the window. I am running IE 7.  Providing the mime type for the Office types (Excel's is "application/vnd.ms-excel" I believe) did not seem to help, but I am running the beta for Office 2007, so that might be a security feature to not display them within the browser and it still closed the window.

For the full list of "official" types: <a href="http://www.iana.org/assignments/media-types/">click here</a>


here are some useful links
<a href="http://www.beansoftware.com/ASP.NET-Tutorials/PDF-View-Custom-Control.aspx">http://www.beansoftware.com/ASP.NET-Tutorials/PDF-View-Custom-Control.aspx</a><a href="http://www.beansoftware.com/ASP.NET-Tutorials/PDF-View-Custom-Control.aspx" target="_blank" title="New Window"></a>

<a href="http://forums.asp.net/t/1068814.aspx">another useful link</a>


这篇关于使用asp.net C#打开PDF文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆