asp.net 如何实现将服务器上的文件下载到本地?

2024-11-01 16:21:46
推荐回答(1个)
回答1:

给你提供一点代码:

string fileURL = this.Server.MapPath("你要下载的文件路径");//文件路径,可用相对路径

FileInfo fileInfo = new FileInfo(fileURL);

Response.Clear();

Response.AddHeader("content-disposition", "attachment;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(fileURL);