var webRootPath = _webHostEnvironment.WebRootPath;
//var webRootPath = _webHostEnvironment.ContentRootPath;//>>>相当于HttpContext.Current.Server.MapPath("")
//文件所在完整路径
var filePath = $"{webRootPath}/{文件路径}";//指定路径字符串的扩展名(包括句点“.”)
string fileExt = Path.GetExtension(文件路径);
var stream = System.IO.File.OpenRead(filePath); //Path.GetExtension
//获取文件的ContentType
var provider = new FileExtensionContentTypeProvider();
//文件扩展名到 MIME 内容类型的映射。
var mime = provider.Mappings[fileExt];
// var downloadName = Path.GetFileName(addrUrl);
return File(stream, mime, 文件名称);
C#文件下载(返回文件流)
于 2022-06-13 15:59:06 首次发布
本文探讨了如何使用ASP.NET环境中获取文件路径、读取文件并确定其Content-Type类型,涉及变量webRootPath、FilePath的使用以及FileExtensionContentTypeProvider的映射。
3535

被折叠的 条评论
为什么被折叠?



