(转)远程下载文件源码
原文地址:
http://www.cnblogs.com/youngerliu/archive/2012/03/07/2384195.html
/********** Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/octet-stream" ; Response.AddHeader( "Content-Disposition" , "attachment; filename=logo.gif" ); System.Net.HttpWebRequest request = "http://dotnet.aspx.cc/Images/logoSite.gif" ); System.Net.HttpWebResponse response = Stream stream = byte [] bytes = new byte [response.ContentLength]; stream.Read(bytes, 0, HttpContext.Current.Response.BinaryWrite(bytes); Response.Flush(); Response.Close(); /********** 大文件************/ HttpWebRequest request = "http://www.xljsf.com//admin/UploadFile/20076222121122.wma" ); request.Timeout = 150000; HttpWebResponse response = Stream stream Response.Clear(); Response.ContentType = "application/octet-stream" ; Response.AddHeader( "Content-Disposition" , "attachment;filename=20076222121122.wma" ); int buffer = 1024; while ( true ) { byte [] bytes = new byte [buffer]; int alreadyRead = stream.Read(bytes, 0, buffer); if (alreadyRead == 0) break ; if (alreadyRead == buffer) Response.BinaryWrite(bytes); else { byte [] lastBytes = new byte [alreadyRead]; for ( int i = 0; i < alreadyRead; i++) lastBytes[i] = Response.BinaryWrite(lastBytes); } } Response.End(); |