在浏览器中打开pdf时提供打开/保存选项

时间:2011-08-05 08:26:36

标签: asp.net-mvc-3 pdf pdf-generation

当我的用户点击链接时,我想为pdf提供标准的打开/保存对话框,而不是在浏览器中打开...使用ffg代码生成pdf:

 Document document = new Document();
        MemoryStream stream = new MemoryStream();
        try
        {
            PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
            pdfWriter.CloseStream = false;

            document.Open();
            document.Add(new Paragraph("Hello World"));
        }
        catch (DocumentException de)
        {
            Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Console.Error.WriteLine(ioe.Message);
        }

        document.Close();

        stream.Flush(); 
        stream.Position = 0; 
        return File(stream, "application/pdf");

2 个答案:

答案 0 :(得分:1)

使用File方法的第三个参数返回时,尝试为文件提供名称:

return File(stream, "application/pdf", "report.pdf");

答案 1 :(得分:0)

    Document document = new Document();
    FileStream stream = new FileStream("report.pdf", FileMode.Create);
    try
    {
        PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
        pdfWriter.CloseStream = false;

        document.Open();
        document.Add(new Paragraph("Hello World"));
    }
    catch (DocumentException de)
    {
        Console.Error.WriteLine(de.Message);
    }
    catch (IOException ioe)
    {
        Console.Error.WriteLine(ioe.Message);
    }

    document.Close();

    stream.Flush(); 
    stream.Position = 0; 
    return File(stream, "application/pdf");