1

私はここでこの記事をフォローしようとしています:http ://www.4guysfromrolla.com/articles/030911-1.aspx

私はServices.asmxにこのメソッドを持っています:

[WebMethod]
public void CreatePdf()
{
    // Create a Document object
    var document = new Document(PageSize.A4, 50, 50, 25, 25);

    // Create a new PdfWriter object, specifying the output stream
    var output = new MemoryStream();
    var writer = PdfWriter.GetInstance(document, output);

    // Open the Document for writing
    document.Open();

    // Create a new Paragraph object with the text, "Hello, World!"
    var welcomeParagraph = new Paragraph("Hello, World!");

    // Add the Paragraph object to the document
    document.Add(welcomeParagraph);

    // Close the Document - this saves the document contents to the output stream
    document.Close();

    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.AddHeader("Content-Disposition",
        "attachment;filename=file.pdf");
    HttpContext.Current.Response.BinaryWrite(output.ToArray());        
}

そして私のページのこのjQueryコード:

$('a.download').click(function () {

    $.ajax({
        type: "POST",
        url: "/Services.asmx/CreatePdf",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result.d);
        }
    });
});

これは、PDFを作成し、それをユーザーのブラウザにストリーミングすることになっています。

クラスとのリンクをクリックするとdownload、Webメソッドがヒットし、コードが実行されます。PDFをブラウザにストリーミングしないだけです。

Firebugを見ると、ステータス200のメソッドに投稿され、次の応答が返されます。

%PDF-1.4%����2 0obj<>streamx�+�r�25P�04WI�2P�5��1���BҸ4>>>/コンテンツ20R/親30R >> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000304 00000 n 0000000015 00000 n 0000000392 00000 n 0000000147 00000 n 0000000443 00000 n 000000048800000nトレーラー<< 21ba8d519bb56a2d0ec514bcb9c47169>] >>%iText-5.3.5 startxref 646 %% EOF {"d":null}

私はここで何か間違ったことをしていますか?

4

3 に答える 3

4

マークBは正しいです。サーバー側のコードをpdf出力ストリームで応答させる必要があります。

したがって、ダウンロードリンクを新しいファイル(PDFDownload.aspxなど)にポイントし、CreatePdf関数のコードをPDFDownload.aspx.csのPageLoadに配置します。

于 2013-02-05T20:47:05.577 に答える
1

xmlhttprequestを使用すると、バイナリデータを受信できないようです。(これはjqueryが行うことです)。あなたがform post標準を行うときa href link

動作するはずです。その場合、応答タイプはブラウザによって処理されるため...

あなたがするようにあなたがサーバー上で一致するヘッダーを設定することを確認してください...

contentDisposition = "attachment=\"" name "";
contentType = "application/pdf";

お役に立てれば

于 2013-02-28T15:01:51.263 に答える
1

まあ私はこれをそのようにします:

ページaspx.csのメソッド

    [WebMethod()]
    public static string CreatePdf()
    {

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 30f, 30f, 30f, 30f);
        iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms);

        doc.Open();
        doc.Add(new iTextSharp.text.Chunk("hello world"));
        doc.Close(); 
        // convert ms to byte and Base64
        return System.Convert.ToBase64String(ms.ToArray());


    }

関数jQuery

$("#createPdf").click(function () {
        //call ajax
        $.ajax({ 
            url: "main.aspx/CreatePdf",
            data: '{}',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                //pdf
                var downloadpdf = $('<a id="downloadpdf" download="file.pdf" href="data:application/pdf;base64,' + result.d + '" >');
                $('body').append(downloadpdf);
                document.getElementById("downloadpdf").click();
                $("#downloadpdf").remove();
            },
            error: function (req, status, error) {
                alert(error);
            }
        }); 
    });                                

私はそれが誰かを助けることを願っています!

于 2015-11-13T20:54:39.873 に答える