簡體   English   中英

如何在ASP.NET MVC中修復圖片鏈接

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

我有問題要問你。 為什么不顯示圖片?

html代碼生成以下代碼:

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

我的代碼如下所示:

@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();

就您而言,可以將ImagePath成為:

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

這將為您提供Images文件夾所在的當前目錄的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM