showModalDialog窗口中 <base target="_self" /> 这个加上了,点击下载文件时没有反应,因为以前的项目有很多是这样做的,前台的页面不能改,只能改所调用的下载的方法,下载貌似只能用repose的方法,能否在response中write一个方法,使其在showModalDialog窗口中能够直接下载呀,总的意思就是说前台的界面啊,调用的接口啊,都不能改了,只能改下载的方法,不知道能不能实现呀?
我调试了一下,好像在showModalDialog窗口中点击下载的话,Response.IsClientConnected=false,然后就不能继续了,请大哥大姐们帮忙解决一下啊!
我调试了一下,好像在showModalDialog窗口中点击下载的话,Response.IsClientConnected=false,然后就不能继续了,请大哥大姐们帮忙解决一下啊!
<head runat="server">
<title>无标题页</title>
<base target="_self" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</div>
</form>
</body>
</html>按钮事件
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("download.aspx");
}download.aspx页面代码
protected void Page_Load(object sender, EventArgs e)
{
//是否下载成功
bool b = false; System.IO.Stream iStream = null; //以10K为单位缓存:
byte[] buffer = new Byte[10000]; int length; long dataToRead; //制定文件路径.
string filepath = "D:\\...."; //文件名.
string filename = "文件.doc";
try
{
// 打开文件.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
// 得到文件大小:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", dataToRead.ToString());//文件大小
while (dataToRead > 0)
{
//保证客户端连接
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000); Response.OutputStream.Write(buffer, 0, length);
Response.Flush(); buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//结束循环
dataToRead = -1;
}
} b = true;
}
catch (Exception ex)
{
b = false; }
finally
{
if (iStream != null)
{
//关闭文件
iStream.Close();
}
} }
好像听说可以Response.Write一个方法去实现的
有些变态项目是这样用的,没办法啊。
public static class DownLoad
{
public static void Down(string filepath)
{
System.IO.FileInfo file = new System.IO.FileInfo(filepath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
}
}
用iframe是可以实现的,但是现在的问题是前台页面不能动了,只能改下载的方法,可行否?
就是只改下载的方法,其他的都不要动了
就这个方法,让这个方法在showModalDialog窗口中可行
//是否下载成功
bool b = false; System.IO.Stream iStream = null; //以10K为单位缓存:
byte[] buffer = new Byte[10000]; int length; long dataToRead; //制定文件路径.
string filepath = "D:\\...."; //文件名.
string filename = "文件.doc";
try
{
// 打开文件.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
// 得到文件大小:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", dataToRead.ToString());//文件大小
while (dataToRead > 0)
{
//保证客户端连接
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000); Response.OutputStream.Write(buffer, 0, length);
Response.Flush(); buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//结束循环
dataToRead = -1;
}
} b = true;
}
catch (Exception ex)
{
b = false; }
finally
{
if (iStream != null)
{
//关闭文件
iStream.Close();
}
}