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();

}

猜你喜欢

转载自www.cnblogs.com/WuHZ/p/9767291.html
今日推荐