C#下载Url文件到本地


protected void Page_Load(object sender, EventArgs e)
{
string filePath = Request.Params["FilePath"];
string fileName = HttpUtility.UrlDecode(Request.Params["FileName"]);
if (filePath != null)
{
DownLoad(fileName, filePath);
}
}

public void DownLoad(string FileName, string FilePath)
{
string result = string.Empty;
WebClient wc = new WebClient();
wc.BaseAddress = FilePath;
byte[] bytes;
bytes = wc.DownloadData(wc.BaseAddress);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(
FileName), System.Text.Encoding.UTF8));
MemoryStream ms = new MemoryStream(bytes); ms.WriteTo(Response.OutputStream); ms.Dispose(); ms.Close();

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM