所以我有这个从特定路径下载文件的功能,并且该路径与 wwwroot 位于完全不同的文件夹路径中。该文件夹位于共享网络文件夹中。返回时没有下载任何内容,但我没有收到任何错误,并且确实说文件存在。当我将返回从 PhysicalFile 更改为 File 时,它说文件不存在。
public FileResult DownloadFileFromFolder()
{
IEnumerable results = DataLayer.GetData(111);
foreach (Objects things in results)
{
if (things.ID == num)
{
filepath = things.FilePath;
filepath = filepath+"/copy.xlsx";
break;
}
}
FileInfo myFile = new FileInfo(@filepath);
bool exists = myFile.Exists;
return PhysicalFile( myFile.FullName,"application/octet-stream",
"Returned_file.xlsx");
}
我尝试使用静态文件夹,但我认为我做得不对
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseCors("Policy");
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider("//path/to/folder/"),
RequestPath = new PathString("/folder"),
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Login}/{id?}");
});
}