向 Google Chrome 提供 PDF 文件时出现问题

发布于 2024-12-25 12:43:58 字数 2017 浏览 1 评论 0原文

我使用 ASMX 样式调用编写了一个 Web 服务来提供 PDF 文件。该服务处理作为 POST 操作发送给它的数据,将数据写入响应,并在向标头添加新的 mime 类型后将数据发送回。

PDF 文件是使用 AlivePDF 在 Flex 应用程序的客户端生成的。

它工作了一段时间,但最近开始在 google chrome 中失败 - chrome 不是在新窗口或 PDF 查看器(取决于浏览器的配置)中打开 PDF,而是简单地显示一个空页面。

如果输入流中传递了有效的 PDF 数据,此代码是否有原因无法打开 PDF?

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Print : System.Web.Services.WebService
{


    [WebMethod]
    public string PrintPDF()
    {
        HttpRequest request = HttpContext.Current.Request;
        HttpResponse response = HttpContext.Current.Response;
        string requestMethod = request.Params["method"];
        string requestFilename = request.Params["name"];

        if(!validateRequest(request))
        {
            throw new ArgumentException(String.Format("Error downloading file named '{0}' using disposition '{1}'", requestFilename, requestMethod));
        }
        response.AddHeader("Content-Disposition", "attachment; filename=\"" + requestFilename + "\"");
        byte[] pdf = new byte[request.InputStream.Length];
        request.InputStream.Read(pdf, 0, (int)request.InputStream.Length);
        response.ContentType = "application/pdf";
        response.OutputStream.Write(pdf, 0, (int)request.InputStream.Length);
        response.Flush();
        response.End();
        return "Fail";
    }

    private bool validateRequest(HttpRequest request)
    {
        string requestMethod = request.Params["method"];
        string requestFilename = request.Params["name"];

        Regex cleanFileName = new Regex("[a-zA-Z0-9\\._-]{1, 255}\\.[a-zA-Z0-9]{1, 3}");
        return (requestMethod == "attachment" || requestMethod == "inline") &&
               cleanFileName.Match(requestFilename) != null;
    }
}

I wrote a webservice using ASMX style calls to serve PDF files. The service processes data sent to it as a POST operation, writes the data to the response, and sends the data back after adding a new mime type to the headers.

The PDF files are generated client side in a flex application using AlivePDF.

It's worked fine for a while, but it recently began failing in google chrome - Instead of opening the PDF in either a new window or a PDF viewer (depending on the browser's configuration), chrome simply displays an empty page.

Is there a reason why this code would fail to open a PDF if it has been passed valid PDF data in the input stream?

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Print : System.Web.Services.WebService
{


    [WebMethod]
    public string PrintPDF()
    {
        HttpRequest request = HttpContext.Current.Request;
        HttpResponse response = HttpContext.Current.Response;
        string requestMethod = request.Params["method"];
        string requestFilename = request.Params["name"];

        if(!validateRequest(request))
        {
            throw new ArgumentException(String.Format("Error downloading file named '{0}' using disposition '{1}'", requestFilename, requestMethod));
        }
        response.AddHeader("Content-Disposition", "attachment; filename=\"" + requestFilename + "\"");
        byte[] pdf = new byte[request.InputStream.Length];
        request.InputStream.Read(pdf, 0, (int)request.InputStream.Length);
        response.ContentType = "application/pdf";
        response.OutputStream.Write(pdf, 0, (int)request.InputStream.Length);
        response.Flush();
        response.End();
        return "Fail";
    }

    private bool validateRequest(HttpRequest request)
    {
        string requestMethod = request.Params["method"];
        string requestFilename = request.Params["name"];

        Regex cleanFileName = new Regex("[a-zA-Z0-9\\._-]{1, 255}\\.[a-zA-Z0-9]{1, 3}");
        return (requestMethod == "attachment" || requestMethod == "inline") &&
               cleanFileName.Match(requestFilename) != null;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2025-01-01 12:43:58

这是 Chrome 的常见问题。这与 Chrome 自制的 pdf 查看器非常挑剔有关。

虽然这不能解决显示问题,但您可以强制下载,从而解决可访问性问题。

损坏

Works

This is a common problem with Chrome. It has to do with Chrome's homebrewed pdf viewer being really picky.

While this doesn't fix the display issue, you can force a download, fixing the accessibility issue.

<a href="http://www.domain.com/painful.pdf">Broken</a>

<a href="http://www.domain.com/painful.pdf" download="notsopainful">Works</a>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文