回复: asp.net下载文件出现提示框或者直接显示在浏览器中
这是我之前根据另外一个版本写的代码,跟上面那个有一些区别
///
/// 在页面中显示下载对话框并下载指定的文件,webPage为页面对象引用(一般赋值Page),filePath为下载文件虚拟路径,fileName为对话框中显示的文件名
///
public static void DownloadFile(Page webPage, string filePath, string fileName)
{
HttpResponse Response = webPage.Response;
FileInfo aFile = new FileInfo(webPage.Server.MapPath(filePath));
Response.Clear();
Response.ClearHeaders();
Response.BufferOutput = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", aFile.Length.ToString());
Response.WriteFile(filePath);
Response.Flush();
Response.End();
}
我初学也不太懂这些区别是怎么回事,大家看一下吧