简体   繁体   English

如何在ASP.NET MVC中修复图片链接

[英]how to fix the link to the pictures in asp net mvc

I have question for you. 我有问题要问你。 Why are not pictures showing? 为什么不显示图片?

The html code generates this: html代码生成以下代码:

<img src=" ~/Images/pilka195919444.jpg" width="300" height="300">
<img src=" ~/Images/traffic195919452.png" width="300" height="300">

and my code looks like this: 我的代码如下所示:

@foreach (var item in Model.Images)
{
    <img src="@Url.Content(item.ImagePath)" width="300" height="300"/>
}
                    if (item != null && item.ContentLength > 0)
                    {
                        if (Path.GetExtension(item.FileName).ToLower() == ".gif"
                            || Path.GetExtension(item.FileName).ToLower() == ".jpg"
                            || Path.GetExtension(item.FileName).ToLower() == ".png"
                            || Path.GetExtension(item.FileName).ToLower() == ".jpeg"
                            )
                        {
                            string fileName = Path.GetFileNameWithoutExtension(item.FileName);
                            string extension = Path.GetExtension(item.FileName);
                            fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;


                            ImagesProducts images = new ImagesProducts();
                            images.ProductId = vm.Product.ProductId;
                            images.ImagePath = " ~/Images/" + fileName;
                            db.ImagesProducts.Add(images);
                            db.SaveChanges();

In your case, you can generate the ImagePath as: 就您而言,可以将ImagePath成为:

images.ImagePath= Path.Combine(Directory.GetCurrentDirectory(), @"/Images/", fileName)

This would give you the path of the current directory where the Images folder resides. 这将为您提供Images文件夹所在的当前目录的路径。

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

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