DotNet 控制器返回静态文件 [英] DotNet Controller to Return Static File

查看:38
本文介绍了DotNet 控制器返回静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图告诉控制器在某种情况下返回一个静态文件.我尝试了很多方法Server.MapPath"对我不起作用,所以我需要一个替代方案.我尝试了以下操作,但出现错误:

return File(Url.Content(startupPath), "text/javascript");

错误是:

InvalidOperationException:没有配置文件提供程序来处理提供的文件.Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.GetFileInformation(VirtualFileResult 结果)

我有 app.UseStaticFiles() 和 app.UseSpaStaticFiles();两个都设置了.

解决方案

让控制器返回文件与启用静态文件或静态 spa 文件不同.

静态文件:

来自

I am trying to tell the controller to return a static file in some condition. I tried many ways "Server.MapPath" doesn't work for me so i need an alternative. i tried following but i get an error:

return File(Url.Content(startupPath), "text/javascript");

the error is:

InvalidOperationException: No file provider has been configured to process the supplied file.
Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.GetFileInformation(VirtualFileResult result)

I have app.UseStaticFiles() and app.UseSpaStaticFiles(); both set.

解决方案

Having a controller return a file is something different than enabling static files or static spa files.

Static files:

From the Static files in ASP.NET Core:

Static files are stored within the project's web root directory. The default directory is /wwwroot. (...) The app's web host must be made aware of the content root directory. (...) Static files are accessible via a path relative to the web root. (...) The URI format to access a file in a subfolder is http:///subfolder/.

using the app.UseStaticFiles() will allow anyone to receive files from within your wwwroot directory.

This might satisfy your conditions. If it does not, because you might want to add authentication to receiving the file or return a different file from the same route based on a query parameter or the current user or you need to generate the file, you need to return the file from acontroller.

Returning a file from a controller:

there isn't a lot of microsoft documentation on this, but there are a lot of Q&A's on stackoverflow.

Checkout:


public ActionResult DownloadFile()
{
    string filename = "File.pdf";
    string filepath = AppDomain.CurrentDomain.BaseDirectory + "/Path/To/File/" + filename;
    byte[] filedata = System.IO.File.ReadAllBytes(filepath);
    string contentType = MimeMapping.GetMimeMapping(filepath);

   var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = filename,
        Inline = true,
    };

    Response.AppendHeader("Content-Disposition", cd.ToString());

    return File(filedata, contentType);
}

One of your problems also seems to be finding the file you want to return. You can use Server.MapPath("~/filename.ext") for this. Make sure your file is included in your compiled project by selecting 'Copy if newer' or 'Copy always' in it's properties at 'Copy to Output Directory'.

这篇关于DotNet 控制器返回静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆