【问题标题】:Getting Unwanted output when using Response.BinaryWrite(MyByte) and Context.ApplicationInstance.CompleteRequest()使用 Response.BinaryWrite(MyByte) 和 Context.ApplicationInstance.CompleteRequest() 时获得不需要的输出
【发布时间】:2013-12-16 07:43:28
【问题描述】:

我正在尝试从数据库下载 word 文档 (.docx)。

所以我在下载函数的末尾使用了以下代码

 Response.BinaryWrite(MyByte)
 Context.ApplicationInstance.CompleteRequest()

但它会将不需要的内容写入我的 word 文件,因此每次打开文件时都会抛出错误。

文件已损坏,无法打开

我用谷歌搜索了这个问题,我得到了 Use Response.End() 而不是 Context.ApplicationInstance.CompleteRequest()

但是 Response.End 抛出错误,因为线程被中止。并且文件完美打开。

【问题讨论】:

  • 最后我通过添加 Response.AddHeader("content-length", mytBytes.Length.ToString()) 得到了这个解决方案

标签: .net vb.net download response


【解决方案1】:

我已经尝试过这个来解决我的问题

我只是设法通过替换来解决这个问题

Response.Clear();

Response.ClearContent();
Response.ClearHeaders();

所以整个事情看起来像:

byte[] downloadBytes = doc.GetData();
Response.ClearContent();
Response.ClearHeaders();

Response.Buffer = true;
Response.ContentType = "application/msword";
Response.AddHeader("Content-Length", downloadBytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=myFile.docx");
Response.BinaryWrite(downloadBytes);
Response.Flush();
Response.End();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 2021-09-12
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2012-04-11
    相关资源
    最近更新 更多