网络地址下载文件,浏览器提示保存

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

文件流是能够下载服务器文件,可是不能下载随便给的网络地址文件,因此蛋痛的查找了不少例子,最后发现服务器

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

示例以下app

  /// <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(); 
             
        }
相关文章
相关标签/搜索