/// <summary> /// 下载核心任务对象 /// </summary> /// <param name="ashxRoute">核心传输对象</param> /// <returns>返回任务</returns> public override async Task ExecuteOutAsync(AshxRouteData ashxRoute) { ashxRoute.HttpContext.Response.Headers.Add("Connection", "Keep-Alive"); ashxRoute.HttpContext.Response.Headers.Add("Content-Length", FileStream.Length.ToString()); ashxRoute.HttpContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Name); //await ashxRoute.HttpContext.Response.Body.WriteAsync(Body.AsMemory(0, Body.Length)); //long len = (1024 * 512) > FileStream.Length ? FileStream.Length : (1024 * 512); //byte[] slice = new byte[1024 * 512]; //byte[] slice = new byte[len]; //long seekiength = 0; //do //{ // int i = await FileStream.ReadAsync(slice.AsMemory(0, slice.Length)); // await writeStream.WriteAsync(slice.AsMemory(0, i)); // await writeStream.FlushAsync(); // seekiength += i; // Array.Clear(slice, 0, i); //} while (FileStream.Length > seekiength); //await FileStream.DisposeAsync(); System.IO.Stream writeStream = ashxRoute.HttpContext.Response.Body; await HttpContextExtension.StreamMove(FileStream, writeStream, 1024 * 512); }
/// <summary> /// 实现页面内容的输出(采用异步IO读取) /// </summary> /// <param name="ashxRoute">当前请求对象</param> /// <returns></returns> public override async Task ExecuteOutAsync(AshxRouteData ashxRoute) { Microsoft.AspNetCore.Hosting.IWebHostEnvironment env = ashxRoute.HttpContext.GetService <Microsoft.AspNetCore.Hosting.IWebHostEnvironment>(); Microsoft.Extensions.FileProviders.IFileInfo view; if (env.WebRootPath is null) { string webRootPath = env.ContentRootPath + "\\wwwroot"; System.IO.Directory.CreateDirectory(webRootPath + "\\Views"); env.WebRootPath = webRootPath; env.WebRootFileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(webRootPath); } //string filePath = ""; if (IsView) { //string directory = AppContext.BaseDirectory; //filePath = string.Concat(directory, "ApiView\\", ashxRoute.Controller, '\\', ashxRoute.Action, ".html"); if (string.IsNullOrEmpty(ViewName)) { ViewName = $"Views\\{ashxRoute.Controller}\\{ashxRoute.Action}.html"; } else { ViewName = $"Views\\{ashxRoute.Controller}\\{ViewName}\\{ashxRoute.Action}.html"; } view = env.WebRootFileProvider.GetFileInfo(ViewName); } else { view = env.WebRootFileProvider.GetFileInfo(ViewName); //if (ViewName != null && ViewName[1].Equals(':')) //{ // filePath = ViewName; //} //else //{ // string directory = AppContext.BaseDirectory; // filePath = string.Concat(directory, ViewName);// '\\' $"{directory}/{ViewName}"; //} //System.IO.File.Exists(@"D:\Nixue工作室\Tool.Net\WebTestApp\wwwroot\"+ViewName); //System.IO.File.OpenRead(@"D:\Nixue工作室\Tool.Net\WebTestApp\wwwroot\" + ViewName); } //if (System.IO.File.Exists(filePath)) //{ // filePath = viewName; //} //else //{ // string directory = AppContext.BaseDirectory.Replace("\\", "/"); // filePath = $"{directory}/{viewName}"; //} if (view.Exists) { //byte[] obj = System.IO.File.ReadAllBytes(filePath); //byte[] obj = await System.IO.File.ReadAllBytesAsync(filePath); //await ashxRoute.HttpContext.Response.WriteAsync(obj); System.IO.Stream streamview = view.CreateReadStream(); System.IO.Stream writeStream = ashxRoute.HttpContext.Response.Body; //long len = (1024 * 50) > view.Length ? view.Length : (1024 * 50); //byte[] slice = new byte[len]; //long seekiength = 0; //do //{ // int i = await streamview.ReadAsync(slice.AsMemory(0, slice.Length)); // await writeStream.WriteAsync(slice.AsMemory(0, i)); // await writeStream.FlushAsync(); // seekiength += i; // Array.Clear(slice, 0, i); //} while (view.Length > seekiength); //await streamview.DisposeAsync(); await HttpContextExtension.StreamMove(streamview, writeStream, 1024 * 50); } else { throw new Exception("找不到页面:“(相对路径)wwwroot\\" + ViewName + "”,无法显示!"); } }