下载:方法一:点击下载
方法二:前台加一个按钮控件:
后台写按钮事件btnDownLoad_onclick:
protected void btnDownLoad_onclick(object sender, EventArgs e)
{
try
{
string mapPath = Server.MapPath("..\download");//生成服务器上要下载的文件所在文件夹的路径
string fileName = "file.txt";//要下载的文件的文件名
//文件路径(如果你确定文件在服务器上的路径例如C:\web\download\file1.txt,可以直接写string filePath = "C:\\web\\download\\file1.txt".但是不推荐)
string filePath = System.IO.Path.Combine(mapPath, fileName);
FileInfo info = new FileInfo(filePath);//打开文件流
long fileSize = info.Length;//指定文件大小
//下载操作
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
Response.AddHeader("Content-Length", fileSize.ToString());
Response.WriteFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
Response.Write("");
Logger.Error("", ex);//记错误日志,如果有需要的话
}
}
///
/// 下载--07建站
///
/// 下载路径
public static void DownloadFile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
FileInfo file = new FileInfo(s_fileName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(file.Name, Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
使用A标记,指向要下载的文件就可以实现下载了。