简体   繁体   中英

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:

<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:

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

This would give you the path of the current directory where the Images folder resides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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