大马资讯论坛 - 马来西亚中文资讯平台
标题:
Asp.net C#实现弹出文件下载对话框-Appears File Download Dialog
[打印本页]
作者:
资讯王
时间:
2011-1-14 09:47
标题:
Asp.net C#实现弹出文件下载对话框-Appears File Download Dialog
Asp.net C#实现弹出文件下载对话框-Appears File Download Dialog
以下的范例,是下载excel档案application/vnd.ms-excel
The code below is demo download application/vnd.ms-excel excel file
string url = "ftp://abc.xls";
Response.Clear();
Response.ContentType = "application/vnd.ms-excel"; //Try to hide if others file type
Response.AppendHeader("Content-Disposition", "attachment; filename=abc.xls");//Try to hide if others file type
if (url != null)
{
byte[] _data;
_data = this.LoadFromURL(url);
Response.BinaryWrite(_data);
Response.Flush();
}
复制代码
protected byte[] LoadFromURL(string url)
{
WebRequest wr = WebRequest.Create(url);
byte[] result;
byte[] buffer = new byte[4096];
using (WebResponse response = wr.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, count);
} while (count != 0);
result = ms.ToArray();
}
}
}
return result;
}
复制代码
欢迎光临 大马资讯论坛 - 马来西亚中文资讯平台 (http://freeinfo.com.my/)
Powered by Discuz! X3.3