【问题标题】:On click, generating a file and presenting a Save As dialog box: How to control the default file name单击时,生成文件并显示另存为对话框:如何控制默认文件名
【发布时间】:2010-03-02 14:45:14
【问题描述】:

我想在用户点击它时生成一个 pdf 文件,而不是之前。然后,在生成文档后,理想情况下,我想为用户提供查看文档或下载文档的选项。如果他们选择下载,我想显示带有适当默认文件名的“另存为文件”对话框。如果他们选择查看,我想在新窗口中查看文档,无论用户对 PDF 扩展名的本地设置如何(我不确定用户是否可以控制是否在新窗口或现在打开 pdf就像他们可以为 MS Office 文件扩展名一样。)

以下代码生成文档并向用户显示要保存的对话框,但默认对话框是网页的名称(例如 WebPageName.pdf),而不是我喜欢的默认文件名。

有没有办法控制用于预填充“另存为”对话框的默认文件名?

我不确定如何在一个链接中获得所有这些功能。

Protected Sub imgPdf_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

    SessionVariables.ChinaVisaId = New SqlInt32(CType(CType(sender, ImageButton).CommandArgument, Integer))
    Dim TargetChinaVisa As ChinaVisa = _Order.ChinaVisas.ItemById(SessionVariables.ChinaVisaId)


    Dim DocName As String = TargetChinaVisa.ReferenceNumber & ".pdf"
    Dim PdfPath As String = Server.MapPath("~/Files/") & DocName
    Dim RelativePath As String = "~/Files/" & DocName
    Dim AbsolutePath As String = "http://" & _SiteName & "/Files/" & DocName

    Dim MyPDF As New ChinaVisaPdf(TargetChinaVisa)

    MyPDF.SaveAsPdf(PdfPath)

    Server.Transfer(RelativePath)

End Subs

附带问题:在新窗口中打开文档时,我认为必须使用客户端代码和绝对路径来完成?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    要设置默认文件名,您需要在“Content-Disposition”HTTP 标头中传递建议的文件名,如下所示:

    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", DocName);
    

    那是在 C# 中,它在 VB 中或多或少是相同的语法(我想!)

    【讨论】:

    • 不确定是否有更接近的 VB.NET 等价物,但这似乎可行: Response.AddHeader("Content-Disposition", String.Format("attachment; filename=" & Chr(34 ) & "{0}" & Chr(34), DocName))。非常感谢你。我喜欢这个网站。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    相关资源
    最近更新 更多