gpt4 book ai didi

c# - 如何在 ASP.net MVC 的文件夹中显示保存的图像

转载 作者:太空狗 更新时间:2023-10-29 23:46:00 25 4
gpt4 key购买 nike

我正在将图像文件保存在文件夹/images/profile 中,我想将图像路径保存在数据库中,但不知道如何在 View 中显示图像。我是 MVC 的新手。以下是图片上传的代码。请帮忙。

HomeController.cs

public class HomeController : Controller
{

ImageEntities db = new ImageEntities();
public ActionResult Index()
{
return View();
}

public ActionResult FileUpload(HttpPostedFileBase file, tbl_Image model)
{
if (file != null)
{
string pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
Server.MapPath("~/images/profile"), pic);
// file is uploaded
file.SaveAs(path);

}

return View("FileUploaded", db.tbl_Image.ToList());

}

public ActionResult FileUploaded()
{
return View();
}

}

文件上传.cshtml

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, 
new { enctype = "multipart/form-data" }))
{
<label for="file">Upload Image:</label>
<input type="file" name="file" id="file" style="width: 100%;" />
<input type="submit" value="Upload" class="submit" />
}

文件上传 View .cshtml

@foreach (var item in Model)
{
<img src="~/images/profile/@item.imagepath" />
}

最佳答案

您应该在表中保存图像名称 而不是图像路径

然后在 View 中试试这个:

<img src="~/images/profile/@Model.imageUrl" />

“更新”

看这里:

How to save image in database and display it into Views in MVC?

关于c# - 如何在 ASP.net MVC 的文件夹中显示保存的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20824318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com