首页 .NET/Web NETCore 获取服务端某个文件夹下面所有文件集合

NETCore 获取服务端某个文件夹下面所有文件集合

作者:胡同里的砖头 围观群众:347 更新于:2021-12-01

private readonly IHostingEnvironment _hostingEnvironment;

public ExcelController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}

[HttpGet("GetFile")]
public IActionResult GetFile()
{

string url = $"{Request.Scheme}://{Request.Host}{Request.PathBase}/file/";

string path = _hostingEnvironment.ContentRootPath + @"\file\";
try
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] finfo = dir.GetFiles();
string fnames = string.Empty;
List<ExcelModel> ms = new List<ExcelModel>();
for (int i = 0; i < finfo.Length; i++)
{
string weburl = url + finfo[i].Name;
string fileurl= path + finfo[i].Name;
ExcelModel m = new ExcelModel();
m.filename= finfo[i].Name;
m.intime = finfo[i].CreationTime;
//var stream = System.IO.File.OpenRead(fileurl);
//m.stream= File(stream, "application/vnd.android.package-archive", Path.GetFileName(fileurl));
m.url = weburl;
ms.Add(m);
}

var response = new
{
code = 20000,
data = ms
};

return Ok(response);

}
catch (Exception)
{
throw;
}

}

  • 本文标题: NETCore 获取服务端某个文件夹下面所有文件集合
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8