网上查找了很多例子,大多数是流,我只贴出一种简单的,至于复杂的流输出,懒得写,网上也很多例子

文件流是可以下载服务器文件,但是不能下载随便给的网络地址文件,所以蛋痛的查找了很多例子,最后发现

WebClient 的方法DownloadData,可以读取为byte[] 这就给了很大方便,几行代码搞定

示例如下

  /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="s_fileName"></param>
        public void downloadfile(string url)
        {  
            String fileName = url.Substring(url.LastIndexOf("/") + 1);
            WebClient wc = new WebClient();
             byte[]  byti=  wc.DownloadData(url);
            //通知浏览器保存文件,其实也就是输出到浏览器
             Response.Clear();
             Response.ContentType = "application/octet-stream";
             Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); 
             Response.BinaryWrite(byti);
             Response.Flush();
             Response.Close(); 
             
        }

 

posted on 2016-08-18 17:18  幻想时空  阅读(261)  评论(0编辑  收藏  举报