将二进制写入响应后回发无响应
No response in postback after writing binary to the response
要求:
我想点击一个按钮下载一个文件(*.docx),首先我读取文件的字节,然后我将字节写入响应,这工作正常而且我之前已经多次使用过这段代码,但是我得到了代码成功执行的这种奇怪的行为,没有例外,但是没有响应被发送回客户端(没有任何反应,没有响应,没有文件下载),此外,如果我把代码放在 Page_Load 事件,代码工作完美,它只在按钮单击事件(即回发)中不起作用,这是我正在使用的代码:
Dim arrBytes() = File.ReadAllBytes(strFilePath)
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=test.docx")
Response.AddHeader("Content-Length", arrBytes.Length.ToString())
Response.BinaryWrite(arrBytes)
Response.Flush()
Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
非常感谢任何帮助。
编辑:
我尝试了 'Response.WriteFile
而不是 Response.BinaryWrite
和 Response.End
而不是 ApplicationInstance.CompleteRequest
但没有成功。
原来 MasterPage 中有一个 UpdatePanel,当我将按钮注册为 post 后退控件时问题就解决了......我知道这很痛苦。
所以我在Page_Load
事件中使用这行代码解决了这个问题:
ScriptManager.GetCurrent(Me).RegisterPostBackControl(btnSubmit)
要求: 我想点击一个按钮下载一个文件(*.docx),首先我读取文件的字节,然后我将字节写入响应,这工作正常而且我之前已经多次使用过这段代码,但是我得到了代码成功执行的这种奇怪的行为,没有例外,但是没有响应被发送回客户端(没有任何反应,没有响应,没有文件下载),此外,如果我把代码放在 Page_Load 事件,代码工作完美,它只在按钮单击事件(即回发)中不起作用,这是我正在使用的代码:
Dim arrBytes() = File.ReadAllBytes(strFilePath)
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=test.docx")
Response.AddHeader("Content-Length", arrBytes.Length.ToString())
Response.BinaryWrite(arrBytes)
Response.Flush()
Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
非常感谢任何帮助。
编辑:
我尝试了 'Response.WriteFile
而不是 Response.BinaryWrite
和 Response.End
而不是 ApplicationInstance.CompleteRequest
但没有成功。
原来 MasterPage 中有一个 UpdatePanel,当我将按钮注册为 post 后退控件时问题就解决了......我知道这很痛苦。
所以我在Page_Load
事件中使用这行代码解决了这个问题:
ScriptManager.GetCurrent(Me).RegisterPostBackControl(btnSubmit)