System.IO.FileInfo DownloadFile = new System.IO.FileInfo(docName.ToString());
// 需要转换为绝对路径,否则会自动认到C盘系统里那个IIS目录下面去,而且,无法通过URI的方式来进行数据流读取。如果你生成的文件不在web目录下,也需要明确指出。
// 下面到就是读取文件,通过数据流的方式下载了。
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.ContentType = "application/octet-stream";
//attachment 参数表示作为附件下载,您可以改成 online在线打开
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
if (System.IO.File.Exists(docName.ToString()))
{
System.IO.File.Delete(docName.ToString());
}
HttpContext.Current.Response.Flush();