版权声明:本文为博主原创文章,转载时请注明出处。 https://blog.csdn.net/nxw_tsp/article/details/85230859
C#(服务器端)通过文件流下载日志文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using xL.WAF;
using Nestle.Import.WebApi.Code;
using System.IO;
using System.Text;
namespace Nestle.Import.WebApi.Controller
{
[WebApi]
[Alias("Downlog")]
[Auth]
public class logController : BaseController
{
/// <summary>
/// 通过文件流下载日志文件
/// </summary>
///<remarks>2018-12-24</remarks>
[Action]
public string download_log(){
string relativePath = HttpRuntime.AppDomainAppPath.ToString();//获取相对路径
string sFileName = "Log_" + DateTime.Now.ToString("yyyy-MM-dd") + "_log_INFO.txt";
string FileNamePath = relativePath + "logs\\" + sFileName; //文件的绝对路径
string filePath = FileNamePath;
FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + sFileName);//下载后的文件命名
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
return "文件已下载成功!";
}
}
}