将网址保存到数据库后显示图像

时间:2019-10-29 02:40:10

标签: c# asp.net model-view-controller core

我正在尝试将图像和视频保存到单独的文件夹中,而不是在SQL中保存,这是我所阅读的最佳实践,不保存在那里。我可以拍照,然后将URL字符串保存到数据库中。文件保存到正确的位置。

拉起视图以显示图像后,它没有显示出来。我可以看到它是一个断开的链接。但是不确定如何纠正它。

控制器:

[HttpPost]
    public  IActionResult AddPost(AddPostViewModel addPost, IFormFile file)
    {
        //Setting the extra data need for the post
        addPost.post.DateModified = DateTime.Now;
        addPost.post.DatePosted = DateTime.Now;

        //Saving the image to a location 
        SaveImage saveImage = new SaveImage();
        Task<String> filePath = saveImage.UploadFile(file);

        addPost.post.PostImageVideo = filePath.Result;



        //Adding and saving the post to the database and then returning to the admin panel. 
        _context.Post.Add(addPost.post);
        _context.SaveChanges();

        ViewBag.SessionUserFirstName = HttpContext.Session.GetString("UserFirstName");
        ViewBag.isAdmin = HttpContext.Session.GetString("IsAdmin");
        return RedirectToAction("AdminPanel");
    }

被调用来保存图像的方法:

public async Task<String> UploadFile(IFormFile file)
    {
        var fileName = "";
        var filePath = "";
        //adding the filepath to the database and saving the path. 
        if (file != null && file.Length > 0)
        {
             fileName = Path.GetFileName(file.FileName);
             filePath = Path.Combine(Directory.GetCurrentDirectory(),@"~/wwwroot/images/", fileName);

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(fileStream);
            }
        }


        return filePath.ToString();

    }

然后从实体中提取链接或从数据库中提取视图的视图

@{
    ViewData["Title"] = "Post";
}

<div class="container">
    <div class="row">
        <div class="col-md-12 text-center">
            <h1>@Model.post.Title</h1>
        </div>
    </div>
    <div class="row">
        <!--This is grabbing the link for the post.-->
        <img src="@Model.post.PostImageVideo" />
        <div class="col-md-12">
            <p>@Model.post.Body</p>
        </div>
    </div>
</div>

当我查看链接时,似乎并没有抓住它所在的wwwroot文件夹,它只是进入图像。不知道如何纠正它。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我想我拥有它:

当我调用saveImage方法时,我正在返回文件路径。我让它返回文件名,并在前面加上其他字符串以将其保存到数据库,如下所示:

 addPost.post.PostImageVideo = "/images/" + fileName.Result;

这是否正确,不确定,但是可以。

答案 1 :(得分:0)

您可以使用FileContentResult将图像作为文件返回以查看。

public class ImageController : Controller
{
    private readonly IHostingEnvironment _appEnvironment;

    public ImageController(IHostingEnvironment appEnvironment)
    {
        _appEnvironment = appEnvironment;
    }

    public IActionResult Index()
    {
        return View();
    }

    public FileContentResult ShowImage()
    {
        string fileName = Path.Combine(_appEnvironment.WebRootPath, "images\\avatar.png").Replace("\\", "/");

        byte[] imageData = null;
        FileInfo fileInfo = new FileInfo(fileName);
        long imageFileLength = fileInfo.Length;
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        imageData = br.ReadBytes((int)imageFileLength);

        return File(imageData, "image/png");
    }
}

查看。

<img src="@Url.Action("ShowImage", "Image", new { area = "" })" class="user-image" alt="User Image">