我的项目与NerdDinner非常相似,我使用PdfSharp生成pdf文档。阻止ActionResult发布到新页面?
我认为我使用这个:
<%: Html.ActionLink("Pdf", "GeneratePdf1", "Persons")%>
调用此的ActionResult:
public ActionResult GeneratePdf1()
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// Save the document...
const string filename = "HelloWorld.pdf";
document.Save(filename);
Process.Start(filename);
return View();
}
的几个问题/这个问题:
- 我不想要发布的链接。当你点击链接时,应该只是打开文件,但我不知道该返回什么才能阻止它发布。
- 我还想要显示“保存/打开”对话框。现在PDF文件直接显示。
我想知道为什么你建议**另外,我会将它保存到一个MemoryStream并返回字节数组,而不是使用文件**。 – DanielB 2011-05-19 13:16:57
因为如果将其保存到文件中,它可能与另一个请求具有相同的名称,并且您可能会获得另一个用户的文件。如果您要使用文件,则需要为请求唯一命名。 – 2011-05-19 13:19:45
好吧,我错过了'document.Save(filename)'静态文件名。这确实非常重要。 Thx开启我的眼睛!这值得+1 ;-) – DanielB 2011-05-19 13:22:56