.net导出excle ajax与response不能一起用,请求解决方案 10
我现在遇到一个问题是这样的,我用ajax导出excle的时候由于用到response.write方法,所以ajax跟response起冲突一个是同步请求一个是异步请求,为...
我现在遇到一个问题是这样的,我用ajax导出excle的时候由于用到response.write方法,所以ajax跟response起冲突一个是同步请求一个是异步请求,为了避免报错我就用了一个空白的页面专门处理导出excle的代码,当点击导出excle按钮时跳转到这个空白页,如果我用的是ie7或者是ie8浏览器的话点击保存或下载这个空白页面就会自动关闭,而ie6就不会,客户的要求必须浏览器是用ie6的,所以请各位高手帮帮忙想想办法将这个ie6的页面关闭。或者有什么好的办法导出excle,不用js。
展开
展开全部
1. 加入一个新Web Form, 命名为DownloadFile.aspx
代码如下:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strFileName As String
Dim strReportName As String
Dim intPosition As Integer
If Request.QueryString("FileName") Is Nothing Then
Response.RedirectErrorPage.aspx?ErrorTitle=505", False)
Exit Sub
End If
strFileName = Server.UrlDecode(Request.QueryString("FileName"))
If strFileName.Trim = "" Then
Response.Redirect(ErrorPage.aspx?ErrorTitle=505", False)
End If
If strFileName.Trim <> "" Then
'get rid of part ahead of "_"
intPosition = strFileName.IndexOf("_")
strReportName = strFileName.Substring(intPosition + 1)
'only download file located in "/DownloadFolder/" folder
strFileName = Server.MapPath("/DownloadFolder/") & strFileName
If IO.File.Exists(strFileName) Then
Dim fi As New IO.FileInfo(strFileName)
'strFileName.IndexOf("Reports") > -1 to avoid attack
'Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment;filename=" & strReportName)
Response.AppendHeader("Content-Length", fi.Length.ToString())
Response.ContentType = "application/ms-excel"
Response.WriteFile(strFileName, 0, fi.Length)
'Response.Flush()
'Response.End()
'close immediately after show save dialog
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "CloseDownloadWindow", "<script type='text/javascript'> window.close(); </script>", False)
Else
Response.Redirect("/ErrorPage.aspx?ErrorTitle=505", False)
End If
End If
End Sub
然后在下载的代码中写:
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "OpenDownloadWindow", String.Format("<script type='text/javascript'> location.href='/DownloadFile.aspx?FileName={0}';</script>", Server.UrlEncode(strFileName)), False)
大概代码就是以上,自己改改吧,不行的话可以hi我
代码如下:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strFileName As String
Dim strReportName As String
Dim intPosition As Integer
If Request.QueryString("FileName") Is Nothing Then
Response.RedirectErrorPage.aspx?ErrorTitle=505", False)
Exit Sub
End If
strFileName = Server.UrlDecode(Request.QueryString("FileName"))
If strFileName.Trim = "" Then
Response.Redirect(ErrorPage.aspx?ErrorTitle=505", False)
End If
If strFileName.Trim <> "" Then
'get rid of part ahead of "_"
intPosition = strFileName.IndexOf("_")
strReportName = strFileName.Substring(intPosition + 1)
'only download file located in "/DownloadFolder/" folder
strFileName = Server.MapPath("/DownloadFolder/") & strFileName
If IO.File.Exists(strFileName) Then
Dim fi As New IO.FileInfo(strFileName)
'strFileName.IndexOf("Reports") > -1 to avoid attack
'Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment;filename=" & strReportName)
Response.AppendHeader("Content-Length", fi.Length.ToString())
Response.ContentType = "application/ms-excel"
Response.WriteFile(strFileName, 0, fi.Length)
'Response.Flush()
'Response.End()
'close immediately after show save dialog
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "CloseDownloadWindow", "<script type='text/javascript'> window.close(); </script>", False)
Else
Response.Redirect("/ErrorPage.aspx?ErrorTitle=505", False)
End If
End If
End Sub
然后在下载的代码中写:
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "OpenDownloadWindow", String.Format("<script type='text/javascript'> location.href='/DownloadFile.aspx?FileName={0}';</script>", Server.UrlEncode(strFileName)), False)
大概代码就是以上,自己改改吧,不行的话可以hi我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询