【问题标题】:pdf file not saving after creationpdf文件创建后不保存
【发布时间】:2016-05-09 05:08:03
【问题描述】:

我正在我的项目中创建一个 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";
            }
        }

输出:

【问题讨论】:

  • 你用“保存链接”代码做什么?
  • 不,没有任何保存代码,尽管在浏览器中打开任何pdf文件时,浏览器本身都会显示保存、打印等选项
  • 你能添加你的输出截图吗?
  • 这里没有添加图片的选项
  • 编辑您的问题,您将能够添加图片

标签: c# asp.net


【解决方案1】:

那是因为你在设置

Response.ContentType = "application/pdf";

这告诉 Chrome 文件是 PDF,然后它用 PDFjs 打开它,当你点击保存时,它想要保存 PDFjs-Viewer 网页而不是 PDF。

如果您单击下载图标,则可以下载 PDF。
您需要设置 content-disposition "attachment",并且需要将 mime-type 设置为 application/octet-stream。然后它可以在 Chrome(以及 IE 和 Firefox)中运行。

如果您想像在 Chrome 中一样在 IE 中的查看器中显示 PDF,那么您需要设置内联内容处置(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 以确保之后释放资源。

【讨论】:

  • 经过一些编辑。我使用了你的代码,它对我有用,谢谢...... :)
  • 你能帮我解决这个问题吗? stackoverflow.com/questions/37020099/…
  • @Alina Anjum:已回答))
  • 谢谢你,先生 :) 我需要你的支持,我困惑了一个月:(
【解决方案2】:

我将代码修改为:

 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";
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多