显示来自C路径的图像或更改虚拟路径指向的位置 [英] Display images from C path or change where virtual path points to
问题描述
因此,我尝试创建一个显示保存在特定文件夹中的图像的应用程序.每个用户在C驱动器上都有一个不同的文件夹,将从中提取图像.
So I'm attempting to create an application that displays images saved on a certain folder. Each user has a different folder on the C drive from which the images will be pulled from.
当前我正在使用:
@foreach (var imgPath in Directory.GetFiles("C:/Users/me/documents", "*.*"))
{
var img = new FileInfo(imgPath);
<img src="@Url.Content(String.Format("C:/Users/me/documents/{0}", img.Name))" />
}
但是,由于C路径,它不会显示图像,但是在网上搜寻无处不在,除了有人说您可以创建虚拟路径并将其指向物理路径之外,我找不到其他相关的教程.
However this does not display the image because of the C path but scouring the web has led me nowhere other than someone saying you could make a virtual path and point it to a physical one but I could not find a tutorial on that.
我还想使me
部分变量,因为每个用户都有自己的文件夹,但是我不确定是否可以在控制器中处理.
I would like to also make the me
part variable as each user would have their own folder but I'm not sure if this can be handled in a controller.
推荐答案
Url.Content
帮助器方法采用虚拟内容路径访问文件,并返回URL来访问站点中的文件.您不应该向该目录传递物理目录位置.
Url.Content
helper method takes a virtual content path to your file and returns a url to access the file in your site. You should not be passing a physical directory location to that.
您可以考虑将图像保存在Web应用程序中.创建一个名为"Images"的目录,然后可以安全地使用Url.Content方法.
You may consider saving the images in your web app. Create a directory called "Images" and then you can safely use Url.Content method.
例如,
@foreach (var imgPath in Directory.GetFiles(Server.MapPath(Url.Content("~/Images/"))))
{
<img src="@Url.Content("~/Images/"+ @Path.GetFileName(imgPath))" />
}
或者,如果您绝对需要将图像存储在应用程序根目录之外的某个位置(例如,C:\ temp \ images或网络位置),则可以考虑创建一个采用文件名的操作方法,然后读取文件从该位置返回并作为图像返回.
Or If you absolutely need to store the images somewhere outside the app root,(Ex : "C:\temp\images or a network location), You might consider creating an action method which takes the file name, read the file from the location and return as an image.
public ActionResult GetImg(string id)
{
var path = $@"C:\temp\images\{id}.png";
var bytes = System.IO.File.ReadAllBytes(path);
return File(bytes, "image/png");
}
现在,您只需要调用此端点并将其用作image src属性即可.
Now you simply need to call this end point and use that as the image src property.
@foreach (var imgPath in Directory.GetFiles(@"C:\temp\images", "*.*"))
{
<img src="@Url.Action("GetImg",new {id=Path.GetFileNameWithoutExtension(imgPath)})" />
}
以上是一个简单的解决方案,其中我对其进行了硬编码以返回png文件.您可以对其进行更新以使其更加灵活(也可以添加参数以接受文件扩展名)
The above is a simple solution where i hard coded it to return png files. you can update it to be more flexible (add a param to accept the file extension as well)
注意:我个人更希望在剃刀文件中保留最少的C#.我喜欢将C#代码(所有Directory.GetFiles
行)移至GET操作,并通过视图模型/视图包(根据需要)将图像名称列表传递给视图.
Note : I personally prefer to keep minimal C# in razor files. I like to move the C# code (all those Directory.GetFiles
lines) to the GET action and pass the list of image names to the view via a view model/view bag(as needed).
这篇关于显示来自C路径的图像或更改虚拟路径指向的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!