简体   繁体   English

如何在asp.net core razor中显示来自数据库文件路径的图像

[英]How to display image from database filepath in asp.net core razor

as the title states.正如标题所说。 I have a dedicated directory in the wwwroot directory.我在 wwwroot 目录中有一个专用目录。 the backend initially stores the whole path from the C: Drive then i changed it to the root directory.后端最初存储来自 C: 驱动器的整个路径,然后我将其更改为根目录。 Am I suppose to store the path according to the solution explorer directory?我是否应该根据解决方案资源管理器目录存储路径? If I use the full path ie from C: Drive or the path from wwwroot in solution explorer i get the same error.如果我使用完整路径,即来自 C: Drive 或来自解决方案资源管理器中 wwwroot 的路径,我会收到相同的错误。

Upload Logic上传逻辑

 public async Task<IActionResult> OnPostAsync(IFormFile file)

        {

            if (!ModelState.IsValid)
            {
                return Page();
            }

            var UserDb = await _context.User.FindAsync(User.ID);



            string folderName = "wwwroot/Uploads/Profile/" + UserDb.ID;
            string webRootPath = _hostEnvironment.ContentRootPath;
            string newPath = Path.Combine(webRootPath, folderName);
            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            string fileName = UserDb.ID + ".jpg";
            string fullPath = Path.Combine(newPath, fileName);
            Console.WriteLine(fullPath);
            string envpath = folderName + "/" + fileName;
            using (var stream = new FileStream(fullPath, FileMode.Create))
            {
                file.CopyTo(stream);
                stream.Flush();

            }


            UserDb.ImageUrl = folderName;
            _context.Attach(UserDb).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(User.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            var currentID = UserDb.ID;
            return RedirectToPage("Socials", new { id = currentID });
        }

        private bool UserExists(int id)
        {
            return _context.User.Any(e => e.ID == id);
        }




    }

Trying to Display The Image尝试显示图像

在此处输入图片说明

 <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.User.ImageUrl)
    </dt>
    <dd class="col-sm-10">
       @Html.DisplayFor(model => model.User.ImageUrl)
        <img src="@Url.Content(@Model.User.ImageUrl)" />
    </dd>

Solution Explorer解决方案资源管理器

在此处输入图片说明

Your @Model.User.ImageUrl should equal to /Uploads/Profile/1018/1018.jpg .你的@Model.User.ImageUrl应该等于/Uploads/Profile/1018/1018.jpg Be sure add single slash at the beginning and specific the file name .确保在开头添加单斜杠指定文件名

So maybe you need change your backend code like below:因此,也许您需要更改后端代码,如下所示:

string folderName = "Uploads/Profile/" + UserDb.ID;    //"Uploads/Profile/2017"
                                   //change here...
string webRootPath = _hostEnvironment.WebRootPath;    //"C:\\YourSolutionLocation\\wwwroot"   
string newPath = Path.Combine(webRootPath, folderName);//"C:\\YourSolutionLocation\\wwwroot\\Uploads/Profile/2017"
//....
string envpath = folderName + "/" + fileName;  //"Uploads/Profile/2017/2017.jpg"
//....
UserDb.ImageUrl = envpath;    

Then change the razor pages like below:然后更改剃刀页面,如下所示:

<img src="/@Url.Content(@Model.User.ImageUrl)" />

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

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