【问题标题】:Change PDF File name while Print - Save as action打印时更改 PDF 文件名 - 另存为操作
【发布时间】:2021-10-27 12:31:11
【问题描述】:

我在 iframe 窗口中显示 PDF 文件,我的下一个操作是使用 javascript 打印该 iframe。文件正在打印窗口中打开,但单击 - 在打印窗口中另存为 PDF 默认选项后,它在窗口对话框中显示 actionname( 方法名称) DownloadReport 作为文件名。我必须在 Windows 对话框中显示不同的文件名。在这里我正在打印员工 pdf 报告,我必须将文件名设置为employeeId。该怎么做?

下面是打印的javascript代码-

$('#printFrame').attr('src', "/Employee/DownloadReport?id=" + empid + "&isEmp=" + flag + "&act=print");

            $('#printFrame').load(function () {
                
                window.frames['frm'].focus();
                window.frames['frm'].print();
                
                
            });

【问题讨论】:

    标签: javascript c# asp.net-mvc iframe


    【解决方案1】:

    使用此方法返回 Pdf 响应

       private FileResult ExportDocument(byte[] document, string format, string fileName, bool isInline)
            {
                string contentType;
                string disposition = (isInline) ? "inline" : "attachment";
    
                switch (format.ToLower())
                {
                    case "docx":
                        contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                        break;
                    case "xls":
                        contentType = "application/vnd.ms-excel";
                        break;
                    case "xlsx":
                        contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        break;
                    case "mht":
                        contentType = "message/rfc822";
                        break;
                    case "html":
                        contentType = "text/html";
                        break;
                    case "txt":
                    case "csv":
                        contentType = "text/plain";
                        break;
                    case "png":
                        contentType = "image/png";
                        break;
    
                    case "Jpeg":
                        contentType = "image/Jpeg";
                        break;
                    default:
                        contentType = String.Format("application/{0};charset=utf-8", format);
                        break;
                }
    
                Response.AddHeader("Content-Disposition", String.Format("{0}; filename={1}", disposition, fileName));
                return File(document, contentType);
            }
    

    你可以像这样使用它

    return ExportDocument(fromFileOrMemoryStream.ToArray(), "pdf", "myCustomReportName", true);
    

    【讨论】:

    • 你没有得到我的问题。我在打印窗口操作中遇到问题。单击另存为 pdf 时 - 文件名将作为操作名/方法名出现。我想要差异。保存文件的窗口对话框中的文件名。
    • 我找到你了兄弟,看看你会找到的解决方案:)
    • 它不工作。我试过你的方法。在打印窗口中单击另存为选项时,它仍然在窗口对话框中将方法名显示为文件名。
    猜你喜欢
    • 2019-11-30
    • 2016-03-16
    • 2019-11-27
    • 1970-01-01
    • 2012-01-10
    • 2021-02-14
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    相关资源
    最近更新 更多