public void Download(string path) //要下载文件的名称
{
if (!string.IsNullOrEmpty(path))
{
string filePath = Server.MapPath("") + path;//路径
// string filePath =@"C:\Users\Public\Pictures\Sample Pictures\1.jpg";
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(".xlsx"));// HttpUtility.UrlEncode(".xlsx")保存文件名,防止乱码
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}
}
protected void btnDownload_Click(object sender, EventArgs e)
{
Download("/.xlsx");//文件地址
}