【问题标题】:ashx page opens in excel instead of document retrievedashx 页面在 excel 中打开,而不是检索到的文档
【发布时间】:2016-12-27 17:34:35
【问题描述】:

我有调用 javascript 函数“GetDocument”的链接,该函数将用户想要检索的链接的 ID 传递到 ashx 页面,然后从数据库中检索文档并将其写回用户浏览器,如果它是PDF 或打开相应的程序(如果是其他内容)。这些可能是 PDF、XLS、DOCX.... 等。当用户单击 PDF 链接时,一切正常,并且 PDF 在浏览器中打开。但是,当用户打开其他任何内容时,例如 xlsx excel 会打开一个带有 .ashx 页面名称的垃圾文件。没有错误发生,一切都适用于 PDF。我有点不知所措。

这里是javascript

function GetDocument(id) {
    spl1.loadPage('RightContent', 'FrmDocHandler.ashx?ID=' + id);
}

这是 .ashx 页面

Public Class FrmDocHandler
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim sID As String = context.Request.QueryString("id")

        Dim fileName As String = String.Empty
        Dim fileType As String = String.Empty
        Dim bytes() As Byte

        bytes = Get_Blob(fileName, fileType, sSql_GetDocument(sID))
        context.Response.Clear()
        'clear the content of the browser
        context.Response.ClearContent()
        context.Response.ClearHeaders()
        context.Response.Buffer = True

        'I tried both of these add header and the same result
        'context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
        context.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName)

        context.Response.ContentType = GetMIMEType(fileType)

        context.Response.BinaryWrite(bytes)


    End Sub

GetMIMEType 返回的 MIME 类型

Public Const g_MIME_DOC As String = "application/msword"
    Public Const g_MIME_DOCX As String = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    Public Const g_MIME_DOT As String = "application/msword"
    Public Const g_MIME_DOTX As String = "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
    Public Const g_MIME_HTM As String = "text/html"
    Public Const g_MIME_HTML As String = "text/html"
    Public Const g_MIME_JPEG As String = "image/jpeg"
    Public Const g_MIME_PDF As String = "application/pdf"
    Public Const g_MIME_PPSX As String = "application/vnd.openxmlformats-officedocument.presentationml.slideshow"
    Public Const g_MIME_PPT As String = "application/vnd.ms-powerpoint"
    Public Const g_MIME_PPTX As String = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    Public Const g_MIME_XLS As String = "application/vnd.ms-excel"
    Public Const g_MIME_XLSX As String = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Public Const g_MIME_XLTX As String = "application/vnd.openxmlformats-officedocument.spreadsheetml.template"
    Public Const g_MIME_XML As String = "application/rss+xml"

【问题讨论】:

    标签: javascript asp.net vb.net


    【解决方案1】:

    可能导致文件在 Excel 中打开的行如下:

    context.Response.ContentType = GetMIMEType(fileType)

    你可以做几件事:

    1. 检查GetMIMEType 返回的 MIME 类型,并确保它是与 PDF 相关的类型 (application/pdf) 而不是与 Excel 相关的类型 (application/vnd.ms-excel)

    2. 在浏览器端检查以查看设置了哪个应用程序来处理您从服务器发送的 mime 类型和/或文件扩展名

    【讨论】:

    • 嗨。当用户选择一个 PDF 链接时,它可以正常工作。当用户选择指向除 PDF 之外的任何内容的链接时,它就会失败。 GetMIMEType 只是一个小函数,它根据文件扩展名返回 MIME 类型。我在上面添加了 MIME 类型
    猜你喜欢
    • 2013-03-25
    • 2019-02-26
    • 2012-07-02
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多