【问题标题】:How to display image from database filepath in asp.net core razor如何在 asp.net core razor 中显示来自数据库文件路径的图像
【发布时间】:2021-09-29 01:08:02
【问题描述】:

如标题所述。我在 wwwroot 目录中有一个专用目录。后端最初存储来自 C: Drive 的整个路径,然后我将其更改为根目录。我是否想根据解决方案资源管理器目录存储路径?如果我在解决方案资源管理器中使用完整路径,即来自 C: Drive 或来自 wwwroot 的路径,我会得到相同的错误。

上传逻辑

 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);
        }




    }

尝试显示图像

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

解决方案资源管理器

【问题讨论】:

  • 您的图片网址不应包含wwwroot。删除它并重试。您始终可以通过在浏览器地址栏中手动输入 URL 来验证文件是否可用。

标签: c# asp.net-core razor-pages


【解决方案1】:

您的@Model.User.ImageUrl 应等于/Uploads/Profile/1018/1018.jpg。请务必在开头添加单斜杠指定文件名

所以也许您需要更改后端代码,如下所示:

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;    

然后像下面这样更改剃须刀页面:

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

【讨论】:

    猜你喜欢
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2013-10-18
    • 2013-04-05
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多