Asp.net C#Asp网络在Chrome 12中下载时出现问题
我的项目有问题。 我有一些下载内容的代码,如:.doc、.zip等 它一直工作正常,直到chrome更新版本12,之后浏览器显示错误消息:“下载中断” 我已经在其他浏览器(Chrome11、FF和IE8)上测试过了,一切正常 代码示例是:Asp.net C#Asp网络在Chrome 12中下载时出现问题,asp.net,google-chrome,download,response,Asp.net,Google Chrome,Download,Response,我的项目有问题。 我有一些下载内容的代码,如:.doc、.zip等 它一直工作正常,直到chrome更新版本12,之后浏览器显示错误消息:“下载中断” 我已经在其他浏览器(Chrome11、FF和IE8)上测试过了,一切正常 代码示例是: HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AppendHeader("Content-Type", "application/msword"); HttpContext
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Type", "application/msword");
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" + filename + ".doc");
HttpContext.Current.Response.AppendHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.Write(strBody);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
有人知道会发生什么,或者我如何解决
注:对不起,我的英语,我是巴西人:)根据添加响应中的回答。End()
解决了这个问题
在你的情况下,应该是
HttpContext.Current.Response.Write(strBody);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
我用这段代码从SQLServer数据库中获取二进制数据,它在所有浏览器中都能工作
DataTable table = SiteFileAccess.GetDataByFileID(fileId);
byte[] b = (byte[])table.Rows[0]["FileData"];
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + table.Rows[0]["FileName"].ToString());
Response.AddHeader("Content-Length", b.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(b);
Response.Flush();
Response.End();
请参阅上面的代码,并根据需要进行修复。如果您需要更多信息,请告诉我。字符串applicationName=Session[“applicationName\u UtilityDetail”]。ToString() 试试这个:
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=yourExcelFileName.xlsx;");
Response.BinaryWrite(yourByteArray);
Response.End();
我假设strBody是一个字符串。您正在将内容传输编码设置为二进制,因此必须使用
Response.BinaryWrite(byteArray)
或删除该行。文件名来自何处,我的意思是它是数据库字段响应。End()将引发异常,因为Flush()已关闭流。1)如果您正在放入代码,可以使用编辑器窗口将其格式化为代码。2) 添加一个小的解释,使其更具上下文和描述性。
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=yourExcelFileName.xlsx;");
Response.BinaryWrite(yourByteArray);
Response.End();