大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索
打印 上一主题 下一主题

Asp.net C#实现弹出文件下载对话框-Appears File Download Dialog

[复制链接]
跳转到指定楼层
1#
发表于 2011-1-14 09:47:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Asp.net C#实现弹出文件下载对话框-Appears File Download Dialog
以下的范例,是下载excel档案application/vnd.ms-excel
The code below is demo download application/vnd.ms-excel excel file

  1. string url = "ftp://abc.xls";
  2. Response.Clear();
  3. Response.ContentType = "application/vnd.ms-excel"; //Try to hide if others file type
  4. Response.AppendHeader("Content-Disposition", "attachment; filename=abc.xls");//Try to hide if others file type
  5. if (url != null)
  6. {
  7.         byte[] _data;
  8.         _data = this.LoadFromURL(url);
  9.         Response.BinaryWrite(_data);
  10.         Response.Flush();
  11. }
复制代码

  1.         protected byte[] LoadFromURL(string url)
  2.         {
  3.             WebRequest wr = WebRequest.Create(url);
  4.             byte[] result;
  5.             byte[] buffer = new byte[4096];

  6.             using (WebResponse response = wr.GetResponse())
  7.             {
  8.                 using (Stream responseStream = response.GetResponseStream())
  9.                 {
  10.                     using (MemoryStream ms = new MemoryStream())
  11.                     {
  12.                         int count = 0;
  13.                         do
  14.                         {
  15.                             count = responseStream.Read(buffer, 0, buffer.Length);
  16.                             ms.Write(buffer, 0, count);
  17.                         } while (count != 0);
  18.                         result = ms.ToArray();
  19.                     }
  20.                 }
  21.             }
  22.             return result;
  23.         }
复制代码

手机版|大马资讯论坛  

GMT+8, 2024-4-20 00:03 , Processed in 0.024561 second(s), 13 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表