-----------倾家荡产70分求解excle从服务器保存到客户端问题--------------

Maple99 2005-06-03 02:19:27
奇怪问题向大家讨教,本人用这段代码从本机一个文件保存,可以弹出对话框要下载文件,而且我设置断点,可以读入字节,但是总是保存本页面什么原因???

FileStream myFile = File.OpenRead("D:\\tt.xls");
byte[] fileCont = new byte[myFile.Length];
myFile.Read(fileCont,0,(int)myFile.Length);
Page.Response.Clear();
Page.Response.AddHeader( "Content-Type", "application/vnd.ms-excel" );
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + "tt.xls" );
Page.Response.AddHeader("Content-Length", myFile.Length.ToString());
Page.Response.BinaryWrite(fileCont);
Page.Response.End();
...全文
237 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
Maple99 2005-06-06
  • 打赏
  • 举报
回复
谢谢各位帮助
Maple99 2005-06-06
  • 打赏
  • 举报
回复
估计是我机器有问题,其他人的不会出现此种情况
Maple99 2005-06-06
  • 打赏
  • 举报
回复
点完保存按钮会弹出保存tt.xls吗,我总是弹出保存downloa页面
老戴12345654 2005-06-03
  • 打赏
  • 举报
回复
怎么会不行呢 我的就可以 你直接运行 download.aspx 就可以下载了

代码如下

download.aspx 只需要一句就可以了
<%@ Page language="c#" Codebehind="Download.aspx.cs" AutoEventWireup="false" Inherits="Test1.Download" %>

download.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Test1
{
/// <summary>
/// Download 的摘要说明。
/// </summary>
public class Download : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
FileStream myFile = File.OpenRead("D:\\tt.xls");
byte[] fileCont = new byte[myFile.Length];
myFile.Read(fileCont,0,(int)myFile.Length);
Page.Response.Clear();
Page.Response.AddHeader( "Content-Type", "application/vnd.ms-excel" );
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + "tt.xls" );
Page.Response.AddHeader("Content-Length", myFile.Length.ToString());
Page.Response.BinaryWrite(fileCont);
Page.Response.End();

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

编译直接再IE中打开 download.aspx 就可以了
Maple99 2005-06-03
  • 打赏
  • 举报
回复
有人知道吗,我在顶一下
Maple99 2005-06-03
  • 打赏
  • 举报
回复
还是不行,晕倒
happer6012 2005-06-03
  • 打赏
  • 举报
回复
新建一页面

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Web.function
{
/// <summary>
/// Summary description for ExcelDownload.
/// </summary>
public class ExcelDownload : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if(this.Request.Params["id"] != null)
{
string exceltemp=this.Request.Params["id"].ToString();
String ReportFileExc = exceltemp.Replace(".xml",".xls");
System.IO.File.Move(exceltemp,ReportFileExc);
System.IO.FileInfo fi = new System.IO.FileInfo(ReportFileExc);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",fi.Length.ToString());
Response.WriteFile(fi.FullName);
Response.Flush();
Response.End();
}
if(this.Request.Params["csv"] != null)
{
// HttpServerUtility server = new HttpServerUtility();
string exceltemp=this.Request.Params["csv"].ToString();
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/Vnd.MS-Excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(exceltemp));

Response.WriteFile(exceltemp);
Response.Flush();
Response.End();
}
}

-----------------------------------------------------------
然后再在你下载的页面部分

<iframe id="Iframe1" name="iframe" src="../function/ExcelDownload.aspx" frameBorder="0"
width="0" height="0" runat="server"></iframe>
</form>

加入上面代码,是</form>上面
-----------------------------
cs部分的下载按钮的事件里
Page.RegisterStartupScript("dgscript","<script Language=\"Javascript\">iframe.document.location.href='../function/ExcelDownload.aspx?id=" + exceltemp + "';</script>");
Maple99 2005-06-03
  • 打赏
  • 举报
回复
我改一句你的代码还不可以。我重起一下
string fileName,filePath;
//filePath=HttpContext.Current.Server.MapPath(Request.ApplicationPath)+"/exportfile/bondinfo.xls";
filePath="D:\\5.xls";
fileName="t.xls";
HttpResponse Response = HttpContext.Current.Response;

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

//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.Charset = "GB2312";
//Response.ContentEncoding.EncodingName=EncodingName.


Response.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
Response.ContentType = "application/vnd.ms-excel";


Response.WriteFile(filePath);
Response.End();
layeryli 2005-06-03
  • 打赏
  • 举报
回复
仔细看看你的机器是否运行了一些奇怪的软件
layeryli 2005-06-03
  • 打赏
  • 举报
回复
试试看我的代码
string fileName,filePath;
filePath=HttpContext.Current.Server.MapPath(Request.ApplicationPath)+"/exportfile/bondinfo.xls";
fileName="t.xls";
HttpResponse Response = HttpContext.Current.Response;

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

//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.Charset = "GB2312";
//Response.ContentEncoding.EncodingName=EncodingName.


Response.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
Response.ContentType = "application/vnd.ms-excel";


Response.WriteFile(filePath);
Response.End();
Maple99 2005-06-03
  • 打赏
  • 举报
回复
好奇怪,我这边点完后,弹出保存框,点保存后就再弹出对话框里的保存文件名就是webform1.aspx
了,你的不是吗?难道是我的机器问题。?〉???
layeryli 2005-06-03
  • 打赏
  • 举报
回复
我这边没问题
Maple99 2005-06-03
  • 打赏
  • 举报
回复
to ddangerous169(零点烛光)

不可以。需要的功能是不想显示文件,直接保存。不是练习
Maple99 2005-06-03
  • 打赏
  • 举报
回复
我试一下你的代码
ddangerous169 2005-06-03
  • 打赏
  • 举报
回复
我认为,xls文件不要再读一遍了。
你是要练习呢,还是实现功能。

实现功能的话,可以直接把文件路径连接过来就行了。
用你的代码,加上一句,看行不行。。。
Response.Flush()

Response.End()




layeryli 2005-06-03
  • 打赏
  • 举报
回复

private void Button1_Click(object sender, System.EventArgs e)
{
FileStream myFile = File.OpenRead("E:\\test\\vs\\aspnet\\webapp\\exportfile\\TEST.xls");
byte[] fileCont = new byte[myFile.Length];
myFile.Read(fileCont,0,(int)myFile.Length);
Page.Response.Clear();
Page.Response.AddHeader( "Content-Type", "application/vnd.ms-excel" );
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + "tt.xls" );
Page.Response.AddHeader("Content-Length", myFile.Length.ToString());
Page.Response.BinaryWrite(fileCont);
Page.Response.End();

}

一点问题都没有
Maple99 2005-06-03
  • 打赏
  • 举报
回复
dudu是什么东西,我应该没有安装的
layeryli 2005-06-03
  • 打赏
  • 举报
回复
放到button下载没问题的
是不是客户端装了一些软件有冲突(如DUDU)
Maple99 2005-06-03
  • 打赏
  • 举报
回复
to LaoDai_Net(老代.Net『学无止境』) :
可以把你刚才说的代码发来看看吗
rfwang 2005-06-03
  • 打赏
  • 举报
回复
up
加载更多回复(9)

62,056

社区成员

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

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

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

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