“线程被终止”启动下载时出现运行时错误

发布于 2024-12-05 13:30:49 字数 756 浏览 4 评论 0原文

情况:

在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-12-12 13:30:50

我使用以下代码下载Excel

   FileStream fs = File.OpenRead(path);
   byte[] data = new byte[fs.Length];
   fs.Read(data, 0, (int)fs.Length);
   Response.Buffer = true;
   Response.ContentType = "application/x-msdownload";
   Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName  );
   Response.BinaryWrite(data);

I used following code to download Excel

   FileStream fs = File.OpenRead(path);
   byte[] data = new byte[fs.Length];
   fs.Read(data, 0, (int)fs.Length);
   Response.Buffer = true;
   Response.ContentType = "application/x-msdownload";
   Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName  );
   Response.BinaryWrite(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文