如何download服务器上的Excel文件

Richard345265669 2009-07-30 11:30:32
我在服务器上保存了一个Excel文档,我需要将有对文档的下载功能,我以前写的这个方法在别的地方没有问题,但是移植的时候在 Response.End();出现异常了,“ Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack”,大侠指点下

public void download()
{
string fileName = "RawData" + ".xls";
//清空输出流
Response.Clear();
//在http头中加入文件名信息
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
//定义输出流为mime类型为
Response.ContentType = "application/x-xls";
//从磁盘中读取文件
string FileFolder = Server.MapPath("Excel File");
System.IO.FileStream fs = System.IO.File.OpenRead(FileFolder + "\\" + fileName);
//定义缓冲区大小
byte[] buffer = new byte[1024];
//第一次读取
int i = fs.Read(buffer, 0, buffer.Length);
//如果读取的字节大于0,则使用binaryWrite()不断向客户端输出文件流
while (i > 0)
{
Response.BinaryWrite(buffer);
i = fs.Read(buffer, 0, buffer.Length);
}
//关闭磁盘文件流
fs.Close();
//关闭输出流
Response.End();
}
...全文
167 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
llsen 2009-08-03
  • 打赏
  • 举报
回复
讲文件地址做参数传递到这个画面
画面代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Reflection;
using System.Diagnostics;

public partial class WebForms_Hospital_Default :PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string strPath = Request["Path"].ToString().Trim();
DownloadFile(Page.Response, strPath);
}
catch (Exception ex)
{
this.ShowMessage(ex.Message);
}
}

#region 下载服务器上的文件
/// <summary>
/// 下载服务器上的文件
/// </summary>
/// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
/// <param name="serverPath">服务器上的文件路径</param>
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}
#endregion
}


Richard345265669 2009-08-03
  • 打赏
  • 举报
回复
找过了,没有找到想要的,呵呵
chen_ya_ping 2009-08-03
  • 打赏
  • 举报
回复
cnblogs上去搜索一下。肯定有答案
Richard345265669 2009-08-03
  • 打赏
  • 举报
回复
有没有大侠给个完整的方法啊??
cpp2017 2009-07-30
  • 打赏
  • 举报
回复
直接Response.WriteFile就行了.

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧