从文件系统返回图像 [英] Return image from file system

查看:77
本文介绍了从文件系统返回图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件系统中有成功上传的图像,我想访问它.所以,我写了GET-方法

There is successfully uploaded image in file system and I want to get access to it. So, I have written GET-method

[HttpGet]
public ActionResult Get(string id) {
    var path = $@"d:\smth\upload\{id}.jpeg";
    return File(path, "image/jpeg");
}

我完全确定所需路径中的文件具有所需名称,但是每次尝试创建File(path, "image/jpeg")时,都会出现Could no find file异常.好像我无权访问wwwroot以外的文件夹.也许我错过了使用静态文件文章中的重要内容?

I'm totally sure that there is the file in required path with required name, but anytime a try to create File(path, "image/jpeg") I get Could no find file exception. Seems like i dont have access to folders outside wwwroot. Maybe I have missed something important from working with static file article?

所以,谁能解释一下如何通过GET -method

So, could anyone explain how to return image that stored in file system outside web-server folder via GET-method

推荐答案

虽然@Shyju所说的是正确的,并且File helper方法不接受物理文件路径,但PhysicalFile helper方法却可以(请参见 GitHub源 ).

While what @Shyju says is true and that the File helper method doesn't accept a physical file path, PhysicalFile helper method does (see GitHub source).

[HttpGet]
public ActionResult Get(string id) {
    var path = $@"d:\smth\upload\{id}.jpeg";
    return PhysicalFile(path, "image/jpeg");
}

这篇关于从文件系统返回图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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