引用命名空间
using System.Net; using System.IO;
public string DownLoadFile(){ string url = Request["url"];//要下载的文件url链接 string path = "/Content/File/" + DateTime.Now.ToString("yyyyMMddhhmmss");//要下载到本地的相对路径 string ext="."+url.spilt('.')[1];//获取要下载文件的后缀格式 string downpath = Server.MapPath(path)+ext;//将相对路径转为绝对路径 // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); //创建本地文件写入流 Stream stream = new FileStream(downpath, FileMode.Create); byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr, 0, (int)bArr.Length); while (size > 0) { stream.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, (int)bArr.Length); } stream.Close(); responseStream.Close(); return "succeed"; }