将文件以流的形式另存为下载

//【1】附件为本地磁盘路径(附件可以为图片、pdf、word等等)
string
strFile = Server.MapPath("/images/526763.pdf"); using (FileStream fs = new FileStream(strFile, FileMode.Open)) { byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("base.pdf", System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }

//【2】附件为网络路径
(附件可以为图片、pdf、word等等)
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
byte[] btPageData = wc.DownloadData("http://www.baidu.com/img/bd_logo1.png"); //获取网络附件流
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("base.png", System.Text.Encoding.UTF8));
Response.BinaryWrite(btPageData);
Response.Flush();
Response.End();
string strTargetHtml = Encoding.Default.GetString(btPageData);
wc.Dispose();
 

 

posted @ 2018-11-12 11:40  张伟大  阅读(893)  评论(0编辑  收藏  举报