简体   繁体   English

从 ASP.Net Core 中的 wwwroot/images 获取图像

[英]Get image from wwwroot/images in ASP.Net Core

I have an image in wwwroot/img folder and want to use it in my server side code.我在 wwwroot/img 文件夹中有一个图像,想在我的服务器端代码中使用它。

How can I get the path to this image in code?如何在代码中获取此图像的路径?

The code is like this:代码是这样的:

Graphics graphics = Graphics.FromImage(path)

It would be cleaner to inject an IHostingEnvironment and then either use its WebRootPath or WebRootFileProvider properties.注入IHostingEnvironment然后使用其WebRootPathWebRootFileProvider属性会更干净。

For example in a controller:例如在控制器中:

private readonly IHostingEnvironment env;
public HomeController(IHostingEnvironment env)
{
    this.env = env;
}

public IActionResult About(Guid foo)
{
    var path = env.WebRootFileProvider.GetFileInfo("images/foo.png")?.PhysicalPath
}

In a view you typically want to use Url.Content("images/foo.png") to get the url for that particular file.在视图中,您通常希望使用Url.Content("images/foo.png")来获取该特定文件的 url。 However if you need to access the physical path for some reason then you could follow the same approach:但是,如果您出于某种原因需要访问物理路径,那么您可以采用相同的方法:

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment env
@{ 
 var path = env.WebRootFileProvider.GetFileInfo("images/foo.png")?.PhysicalPath
}

Building on Daniel's answer, but specifically for ASP.Net Core 2.2:基于 Daniel 的回答,但专门针对 ASP.Net Core 2.2:

Use dependency injection in your controller:在控制器中使用依赖注入:

[Route("api/[controller]")]
public class GalleryController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;
    public GalleryController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }        

    // GET api/<controller>/5
    [HttpGet("{id}")]
    public IActionResult Get(int id)
    {
        var path = Path.Combine(_hostingEnvironment.WebRootPath, "images", $"{id}.jpg");
        var imageFileStream = System.IO.File.OpenRead(path);
        return File(imageFileStream, "image/jpeg");
    }
}

A concrete instance of the IHostingEnvironment is injected into your controller, and you can use it to access WebRootPath (wwwroot). IHostingEnvironment 的一个具体实例被注入到您的控制器中,您可以使用它来访问 WebRootPath (wwwroot)。

FYI.Just an update to this.仅供参考。只是对此的更新。 In ASP.NET Core 3 & Net 5 it's the following:在 ASP.NET Core 3 和 Net 5 中,如下所示:

    private readonly IWebHostEnvironment _env;

    public HomeController(IWebHostEnvironment env)
    {
        _env = env;

    }

    public IActionResult About()
    {
      var path = _env.WebRootPath;
    }

This works:这有效:

private readonly IHostingEnvironment env;
public HomeController(IHostingEnvironment env)
{
    this.env = env;
}

public IActionResult About()
{
    var stream = env.WebRootFileProvider.GetFileInfo("image/foo.png").CreateReadStream();
    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
    Graphics graphics = Graphics.FromImage(image);
}
 string path =  $"{Directory.GetCurrentDirectory()}{@"\wwwroot\images"}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM