我们在用的框架是继承自.NET Core 2.0,但里面有很多方法是经过了封装的,暂时并没有源码,所以只能够写成这样,如果代码中有什么错误更是希望有大神能够及时指出,我也能进步一些,毕竟我也是在最近不久才学习并使用.NET Core,还希望大家多多支持一起进步
private void DownLoad(string filepath,RomensHttpContext context,string filename)
{
string fileName=string.IsNullOrEmpty(filename);
//FileInfo fi=new FileInfo(filepath);
//Encoding encoding;
//在我们框架中UserAgent被封装称了一个HttpContext属性
string browser=context.UserAgent.ToUpper();
if(browser.Contains("MS") == true && browser.Contains("IE") == tru)
{
fileName = HttpUtility.UrlEncode(filename);
//encoding = Encoding.Default;
}
else if(browser.Contains("FIREFOX") == true)
{
//encoding = Encoding.GetEncoding("GB2312");
}
else
{
fileName = HttpUtility.UrlEncode(filename);
//encoding = Encoding.Default;
}
//context.Response.ContentType = "excel/csv";
context.Response.Body.Close();
context.Response.Headers["Content-Type"]="application/x-bittorrent";
context.Response.Headers["Content-Disposition"]="attachment; filename="+fileName +".csv";
FileStream t = new FileStream(filepath,FileMode.Open);
byte[] bytes = new byte[(int)t.Length];
context.Response.Headers.ContentLength = t.Length;
t.Read(bytes,0,(int)t.Length);
context.Response.Body.Write(bytes,0,bytes.Length);
context.Response.Body.Flush();
t.Close();
}
这便是HttpContext中关于Body与Headers的运用了,还希望大家能够多多交流,不要吐槽,谢谢!