0

ASMX スタイルの呼び出しを使用して PDF ファイルを提供する Web サービスを作成しました。サービスは、POST 操作として送信されたデータを処理し、データを応答に書き込み、新しい MIME タイプをヘッダーに追加した後にデータを送り返します。

PDF ファイルは、AlivePDF を使用してフレックス アプリケーションでクライアント側で生成されます。

しばらくは問題なく動作していましたが、最近 google chrome で失敗し始めました - 新しいウィンドウまたは PDF ビューアー (ブラウザーの構成によって異なります) で PDF を開く代わりに、chrome は単に空のページを表示します。

入力ストリームで有効な 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;
    }
}
4

1 に答える 1

1

これは、Chrome でよくある問題です。これは、Chrome の自作の pdf ビューアが非常にうるさいことに関係しています。

これで表示の問題は解決しませんが、ダウンロードを強制して、アクセシビリティの問題を解決できます。

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

于 2012-12-04T18:32:06.847 に答える