2013-08-27 61 views
5

我有,我想一个HTML页面导出为PDF文件的ASP.NET应用程序MVC4,我用这个代码,它的正常工作:code导出PDF文件与ASP.NET MVC

该代码将html页面转换为在线PDF,我想直接下载该文件。

如何更改此代码以获得此结果?

+2

请张贴相关的代码。你不应该指望人们去那里阅读整篇文章。 –

回答

5

随着FileContentResult:

protected FileContentResult ViewPdf(string pageTitle, string viewName, object model) 
{ 
    // Render the view html to a string. 
    string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model); 

    // Let the html be rendered into a PDF document through iTextSharp. 
    byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle); 

    // Return the PDF as a binary stream to the client. 
    return File(buffer, "application/pdf","file.pdf"); 
} 
3

让它作为附件,并给它一个文件名返回结果时:

protected ActionResult ViewPdf(string pageTitle, string viewName, object model) 
{ 
    // Render the view html to a string. 
    string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model); 

    // Let the html be rendered into a PDF document through iTextSharp. 
    byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle); 

    // Return the PDF as a binary stream to the client. 
    return File(buffer, "application/pdf", "myfile.pdf"); 
} 

是什么使文件显示为附件和弹出另存为对话框是以下行:

return File(buffer, "application/pdf", "myfile.pdf"); 
1

使用:

这是VB.NET(下面C#)

Public Function PDF() As FileResult 
     Return File("../PDFFile.pdf", "application/pdf") 
    End Function 

在您的操作方法。其中PDFFIle是您的文件名。

对于C#

Public FileResult PDF(){ 
    return File("../PDFFile.pdf", "application/pdf"); 
}