/// <summary>
/// 数据下载
/// </summary>
/// <param name="path">网站文件的绝对路径</param>
/// <returns></returns>
public async Task<IActionResult> DownloadFile(string path)
{
if (string.IsNullOrEmpty(path))
{
return NotFound();
}
var memoryStream = new MemoryStream();
using (var stream = new FileStream(path, FileMode.Open))
{
await stream.CopyToAsync(memoryStream);
}
memoryStream.Seek(0, SeekOrigin.Begin);
string[] strArry = path.Split('/');
string fileName = strArry[strArry.Length - 1];
//文件名必须编码,否则会有特殊字符(如中文)无法在此下载。
string encodeFilename = System.Net.WebUtility.UrlEncode(fileName);
Response.Headers.Add("Content-Disposition", "attachment; filename=" + encodeFilename);
return new FileStreamResult(memoryStream, "application/octet-stream");//文件流方式,指定文件流对应的ContenType。
}
.net core 文件下载 (浏览器弹出下载对话框)
最新推荐文章于 2024-09-26 17:58:50 发布