pdf文件未从网站加载

时间:2015-03-31 14:43:50

标签: c# asp.net

如何打开已保存在客户端服务器上的pdf文件。我正在创建一个这样做的过程,但它没有奏效 (System.Diagnostics.Process.Start(pdfFilePath + pdffileName);)。 我需要在客户端打开这个文件,因为他们需要发送标签打印。

3 个答案:

答案 0 :(得分:0)

看看这里: .NET MVC FileResult equivalent in Web Forms

标记为答案的答案也应符合您的需求。

答案 1 :(得分:0)

尝试将文件写入响应中。您尝试在服务器上打开它。

Context.Response.Buffer = false;
        FileStream inStr = null;
        byte[] buffer = new byte[1024];
        long byteCount; inStr = File.OpenRead(pdfFilePath + pdffileName);
        while ((byteCount = inStr.Read(buffer, 0, buffer.Length)) > 0) {
            if (Context.Response.IsClientConnected) {
                Context.Response.ContentType = "application/pdf";
                Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                Context.Response.Flush();
            }
        }

答案 2 :(得分:0)

您需要将文件流式传输到客户端(如上面的答案所示),或将其保存到网站下的某个位置,并为用户提供指向该位置的链接。您不需要启动进程 - 链接到该文件,浏览器将找出用于打开它的应用程序。

(您可能需要在IIS中设置MIME类型)

例如,假设您在PDF文件下保存在网站中:

HyperLink pdfLink = new Hyperlink();
pdfLink.NavigateUrl = "~/PDFFiles/" + filename

然后将链接添加到页面控件。 (或者您可以对ASPX文件中的链接进行硬编码并在代码隐藏中设置NavigateUrl属性 - 但是您想要这样做)