资讯王 发表于 2011-1-14 09:47:39

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;

            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;
      }
页: [1]
查看完整版本: Asp.net C#实现弹出文件下载对话框-Appears File Download Dialog