public void DownloadFile(string path) { string fileURL = this.Server.MapPath(path);//文件路径,可用相对路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileURL); Response.Clear(); // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "p_w_upload;filename=" + Server.UrlEncode(fileInfo.Name.ToString()));//文件名 // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", fileInfo.Length.ToString());//文件大小 // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.Default; // 把文件流发送到客户端 Response.WriteFile(fileInfo.FullName); Response.End(); }
转载于:https://blog.51cto.com/962410314/1539899