如何从路径中返回iEnummerable照片并显示它们 [英] How to return an iEnummerable of photos from its path and display them
本文介绍了如何从路径中返回iEnummerable照片并显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我上传多个文件,我如何从其路径显示照片。上传工作正常,但我无法显示照片。
我的代码是:
If i upload multiple files, how do i get to display the photos from its path. The Upload is working fine, but i cant display the photo's.
I have this as my code:
[HttpPost]
public ActionResult CreateTopic(IEnumerable<HttpPostedFileBase> files, Post post, int id = 0)
{
//try
//{
if (ModelState.IsValid)
{
Post model = new Post();
model.File = new List<string>();
if (Request.Files.Count > 0)
{
System.Random randomInteger = new System.Random();
int genNumber = randomInteger.Next(1000000);
foreach (var item in files)
{
string fileName = Path.Combine(Server.MapPath("~/Uploads/"), Path.GetFileName(genNumber + item.FileName));
img.Save(fileName);
if (fileName == null)
{
ViewBag.image = "True";
}
model.File.Add(fileName);
}
}
这里是我想要显示照片的地方:
And here is where i want to display the photo's:
<img class="img-responsive" src="/Uploads/@(System.IO.Path.GetFileName(post.PostFile))" />
请问如何从路径显示多个文件?
我尝试过:
Please how do i display multiple files from path?
What I have tried:
<img class="img-responsive" src="/Uploads/@(System.IO.Path.GetFileName(post.PostFile))" />
推荐答案
我假设您要将文件保存到上传文件夹中。创建一个模型来保存你的图像文件名(或者如果你已经使用过它就修改它)
I'm going to assume you're saving the files to the upload folder. Create a model to hold your image filenames (or amend it if you're already using one)
public class UploadModel
{
public List<string> Images { get; set; }
}
保存图像时,将文件名添加到模型中。在这里我很难编码,但你会在你的循环中完成
As you save the images add the filename to the model. Here I'm hard-coding but you'll do it in your loop
public ActionResult Index()
{
UploadModel model = new UploadModel();
model.Images = new List<string>();
model.Images.Add("Image1.jpg");
model.Images.Add("Image2.jpg");
model.Images.Add("Image3.jpg");
return View(model);
}
然后你的观点
Then your view
@model UploadModel
@foreach (string image in Model.Images)
{
<img src="@Url.Content(string.Concat("~/uploads/", image))" />
}
这篇关于如何从路径中返回iEnummerable照片并显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文