“线程被终止”启动下载时出现运行时错误
情况:
在一个 C# 网站项目中,我从数据库中获取数据并将所需数据写入 Excel 文件服务器端,然后我希望提供下载。
问题:
在启动下载的代码末尾(见下文),我收到一个运行时错误,该错误表明线程已终止并且没有提供文件可供下载。
我的代码
FileStream fStream = new FileStream(resultFile, FileMode.Open, FileAccess.Read);
byte[] byteBuffer = new byte[(int)fStream.Length];
fStream.Read(byteBuffer, 0, (int)fStream.Length);
fStream.Close();
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Length", byteBuffer.Length.ToString());
response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(resultFile));
response.TransmitFile(resultFile);
response.End();
我希望有人能帮助我解决这个问题。提前致谢 :)
The situation:
In a C# web site project I am getting data out of a database and write the required data to an excel file server side, which I then want to offer for downloading.
The problem:
At the end of the code to initiate a download (See below) I get a runtime error that the thread is terminated and no file is offered for downloading.
My code
FileStream fStream = new FileStream(resultFile, FileMode.Open, FileAccess.Read);
byte[] byteBuffer = new byte[(int)fStream.Length];
fStream.Read(byteBuffer, 0, (int)fStream.Length);
fStream.Close();
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Length", byteBuffer.Length.ToString());
response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(resultFile));
response.TransmitFile(resultFile);
response.End();
I hope somebody can help me with this. Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用以下代码下载Excel
I used following code to download Excel