我已经将文件保存为数据库中的字节,但我需要将其转换回图像以显示在我的页面上.这是我的控制器

本文关键字:我的 显示 图像 控制器 转换 保存 文件 数据库 字节 | 更新日期: 2024-10-25 04:42:42

我试图让用户上传个人资料图片作为其个人资料的一部分。这篇文章中的图片显示了我如何将其保存为数据库中的字节的代码。现在的问题是在我的页面上显示它。我相信我必须将其从字节转换为图像,但我不知道如何或这是否是正确的方法。谁能帮我,或者有更简单的方法来处理图片上传?谢谢。

    // GET: /Manage/ChangePic
    [HttpGet]
    [AllowAnonymous]
    public async Task<IActionResult> ChangePic()
    {
        var user = await GetCurrentUserAsync();
        var model = new IndexViewModel();
        model.ProfilePic64 = Convert.ToBase64String(user.ProfilePic);
        return View(model);
    }
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ChangePic(IndexViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = await _userManager.FindByIdAsync(User.GetUserId());
            var breader = new BinaryReader(model.ProfilePic.OpenReadStream());
            var byteImage = breader.ReadBytes((int)breader.BaseStream.Length);
            user.ProfilePic = byteImage;
            var result = await _userManager.UpdateAsync(user);
            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                // Send an email with this link
                //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                //    "Please confirm your account by clicking this link: <a href='"" + callbackUrl + "'">link</a>");
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "Profile info updated");
                return RedirectToAction(nameof(ManageController.Index), "Manage");
            }
            AddErrors(result);
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

我已经将文件保存为数据库中的字节,但我需要将其转换回图像以显示在我的页面上.这是我的控制器

您需要

单独的控制器方法,该方法将按记录ID返回图像。以下是一些解决方案:在 asp mvc 中显示数据库中的图像

相关文章: