Javascript ashx页面在excel中打开,而不是检索到文档
我有一个调用javascript函数“GetDocument”的链接,该函数将用户想要检索的链接ID传递到一个ashx页面,然后从数据库检索文档并将其写回用户浏览器(如果是PDF),或者如果是其他内容,则打开相应的程序。这些可以是PDF、XLS、DOCX。。。。等当用户单击一个PDF链接时,一切正常,PDF在浏览器中打开。但是,当用户打开任何其他内容时,比如说,xlsx excel会打开一个名为.ashx页面的垃圾文件。没有错误发生,一切都可以使用PDF。我有点不知所措 下面是javascriptJavascript ashx页面在excel中打开,而不是检索到文档,javascript,asp.net,vb.net,Javascript,Asp.net,Vb.net,我有一个调用javascript函数“GetDocument”的链接,该函数将用户想要检索的链接ID传递到一个ashx页面,然后从数据库检索文档并将其写回用户浏览器(如果是PDF),或者如果是其他内容,则打开相应的程序。这些可以是PDF、XLS、DOCX。。。。等当用户单击一个PDF链接时,一切正常,PDF在浏览器中打开。但是,当用户打开任何其他内容时,比如说,xlsx excel会打开一个名为.ashx页面的垃圾文件。没有错误发生,一切都可以使用PDF。我有点不知所措 下面是javascrip
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"
可能导致文件在Excel中打开的行如下所示:
context.Response.ContentType=GetMIMEType(文件类型)
您可以做以下几件事:
GetMIMEType
返回的MIME类型,并确保它是与PDF相关的类型(application/PDF
),而不是与Excel相关的类型(application/vnd.ms Excel
)你好当用户选择PDF格式的链接时,它会正常工作。当用户选择一个指向除PDF以外的任何内容的链接时,它就失败了。GetMIMEType只是一个小函数,它根据文件扩展名返回MIME类型。我在上面添加了MIME类型