代码之家  ›  专栏  ›  技术社区  ›  NoBullMan

iTextSharp:创建PDF文件时显示“打开/保存”对话框

  •  0
  • NoBullMan  · 技术社区  · 13 年前

    我正在使用iTextSharp创建一个PDF文件(我使用C#)。文件创建良好;但是,它保存在一个文件夹中。该文件是在点击链接按钮后创建的,我想显示“打开/保存”对话框,而不是将字段保存在文件夹中。 我使用以下内容:

    using(MemoryStream ms = new MemoryStream())
    {
        Document doc1 = new Document();
        PdfWriter pdfw = PdfWriter.GetInstance(doc1, ms);
        doc1.open();
        string sPath = Server.MapPath("PDF/");
        string sFileName = "Something.PDF";
    
        // create the PDF content
    
        doc1.Close();
        byte[] content = ms.ToArray();
        using (FileStream fs = FileStream.Create(sPath + sFileName))
        {
            fs.Write(content, 0, (int)content.Length);
        }
    }
    

    我做错了吗?它创建文件并将其保存在“blahbah/PDF”文件夹中,但不显示“打开/保存”对话框bx。

    1 回复  |  直到 13 年前
        1
  •  1
  •   VahidN    13 年前

    如果您已经拥有该文件,请使用 Response.TransmitFile 方法在用户的浏览器中刷新它。

    Response.Clear(); 
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=buylist.pdf");
    Response.TransmitFile(Server.MapPath("~/myfile.pdf"));