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;
- }
复制代码 |
|