62,046
社区成员
发帖
与我相关
我的任务
分享
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode([默认下载文件名], System.Text.Encoding.UTF8));
Response.BinaryWrite([char[] buff]);//buff:文件内容
Response.Flush();
Response.End();
/// <summary>
/// 下载服务器文件至客户端
/// </summary>
/// <param name="url">被下载的文件地址,绝对路径</param>
/// <param name="dir">另存放的目录</param>
public void DownloadUrlFile(string url, string dir)
{
WebClient client = new WebClient();
string fileName = Path.GetFileName(url); //被下载的文件名
string path = dir + fileName; //另存为的绝对路径+文件名
try
{
if (!System.IO.Directory.Exists(dir))
{
System.IO.Directory.CreateDirectory(dir);
}
if (!System.IO.File.Exists(path))
{
client.DownloadFile(url, path);
}
}
catch (Exception)
{
// ShowError("文件下载失败!");
}
}
WebClient client = new WebClient();
client.DownloadFile("地址", "本地存放位置");