我正在我的项目中创建一个pdf文件.Pdf生成工作正常。创建后,它会打开浏览器但是当我点击保存链接时,它会提示保存整个页面而不是pdf文件。可以任何一个人帮我吗? 这是我的代码:
protected void btn_ViewSlip_Click(object sender, EventArgs e)
{
dt_Payslip = mm_salary.GetPayslipData(DDL_Year.SelectedValue.ToString(), DDL_Month.SelectedValue.ToString(), Session["empcd"].ToString());
if (dt_Payslip.Rows.Count > 0)
{
dt_CurrPay = emp_coprofile.GetCurrExp(Session["empcd"].ToString(), DDL_Month.SelectedValue.ToString(), DDL_Year.SelectedValue.ToString());
GenerateDisclaimerPDF();//pdf is generating here
string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() + " - Payslip.pdf";
System.IO.FileInfo file = new System.IO.FileInfo(url);
if (file.Exists)
{
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(url);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
else
{
msg = "Pay slip for "+DDL_Month.SelectedValue.ToString()+"-"+DDL_Year.SelectedValue+" does not exist";
}
}
答案 0 :(得分:1)
那是因为你正在设置
Response.ContentType = "application/pdf";
告诉Chrome这个文件是PDF,然后用PDFjs打开它,当你点击保存时,它想要保存PDFjs-Viewer网页而不是PDF。
如果单击下载图标,则可以下载PDF。
您需要设置content-disposition" attachment",您需要将mime-type设置为application / octet-stream。然后它适用于Chrome(以及IE和Firefox)。
如果您想在IE中的查看器中显示PDF,就像在Chrome中一样,那么您需要设置内联内容(IE忽略mime类型并转而使用文件扩展名)。
Dim baPDF As Byte() = GetPdfFromImage(Me.Data)
context.Response.Clear()
'context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName)
context.Response.AddHeader("Content-Disposition", Portal.ASP.NET.GetContentDisposition("Drucken.pdf", "inline"))
context.Response.AddHeader("Content-Length", baPDF.Length.ToString())
' context.Response.ContentType = "application/msword"
' context.Response.ContentType = "application/octet-stream"
' http://superuser.com/questions/219870/how-to-open-pdf-in-chromes-integrated-viewer-without-downloading-it#
' context.Response.ContentType = "text/html"
context.Response.ContentType = "application/pdf"
另外,请注意文件名中是否包含UTF-8字符,需要正确设置文件名标题(不同版本[IE]中不同浏览器的值不同)。
Public Shared Function GetContentDisposition(ByVal strFileName As String) As String
Return GetContentDisposition(strFileName, "attachment")
End Function ' GetContentDisposition '
' http://www.iana.org/assignments/cont-disp/cont-disp.xhtml '
Public Shared Function GetContentDisposition(ByVal strFileName As String, ByVal strDisposition As String) As String
' http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http '
Dim contentDisposition As String
strFileName = StripInvalidPathChars(strFileName)
If String.IsNullOrEmpty(strDisposition) Then
strDisposition = "inline"
End If
If System.Web.HttpContext.Current IsNot Nothing AndAlso System.Web.HttpContext.Current.Request.Browser IsNot Nothing Then
If (System.Web.HttpContext.Current.Request.Browser.Browser = "IE" And (System.Web.HttpContext.Current.Request.Browser.Version = "7.0" Or System.Web.HttpContext.Current.Request.Browser.Version = "8.0")) Then
contentDisposition = strDisposition + "; filename=" + Uri.EscapeDataString(strFileName).Replace("'", Uri.HexEscape("'"c))
ElseIf (System.Web.HttpContext.Current.Request.Browser.Browser = "Safari") Then
contentDisposition = strDisposition + "; filename=" + strFileName
Else
contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
End If
Else
contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
End If
Return contentDisposition
End Function ' GetContentDisposition '
此外,如果您愿意接受好建议,请摆脱
Session["empcd"]
您正在为NullReferenceExceptions设置自己,而且完全没有必要......
如果可以避免,请尽量不要使用会话。
哦,顺便说一下:
您应该在WebClient中添加using子句;否则你没有妥善处理资源。
using(WebClient client = new WebClient())
{
// Your code goes here
} // End Using
这将比client.dispose()具有额外的好处,你不需要将它放入try-cach if-not-null-dispose以确保之后释放资源。
答案 1 :(得分:0)
我将代码修改为:
protected void btn_ViewSlip_Click(object sender, EventArgs e)
{
dt_Payslip = mm_salary.GetPayslipData(DDL_Year.SelectedValue.ToString(), DDL_Month.SelectedValue.ToString(), Session["empcd"].ToString());
if (dt_Payslip.Rows.Count > 0)
{
dt_CurrPay = emp_coprofile.GetCurrExp(Session["empcd"].ToString(), DDL_Month.SelectedValue.ToString(), DDL_Year.SelectedValue.ToString());
GenerateDisclaimerPDF();
string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() + " - Payslip.pdf";
System.IO.FileInfo file = new System.IO.FileInfo(url);
if (file.Exists)
{
//WebClient client = new WebClient();
//Byte[] buffer = client.DownloadData(url);
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-length", buffer.Length.ToString());
//Response.BinaryWrite(buffer);
// Response.Clear();
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(url);
Response.AddHeader("content-disposition", "attachment; filename=" + Session["empcd"].ToString() + " - Payslip.pdf");
Response.AddHeader("content-length", buffer.Length.ToString());
Response.ContentType = "application/pdf";
Response.BinaryWrite(buffer);
}
}
else
{
msg = "Pay slip for "+DDL_Month.SelectedValue.ToString()+"-"+DDL_Year.SelectedValue+" does not exist";
}
}