从www / Upload / DP / image.png asp.net核心2更新图像

时间:2018-09-22 17:08:59

标签: asp.net-core-2.0

我有一个用于图像上传的代码,该代码可以按预期工作,因此我的图像是用户的DP显示图片。

  

Create.cshtml

<div class="form-group">
                    <label asp-for="DP" class="control-label">Profile Image</label>
                    <input type="file" name="DP" asp-for="DP" class="form-control" />
                    <span asp-validation-for="DP" class="text-danger"></span>
                </div>
  

控制器动作

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(Profile profile, IFormFile DP)
        {
            if (ModelState.IsValid)
            {
                var id = _userManager.GetUserName(HttpContext.User);                
                var fileName = Path.Combine(_environment.WebRootPath +"/Upload/DP/", Path.GetFileName(id+".png"));                
                DP.CopyTo(new FileStream(fileName,FileMode.Create));
                //profile.DP = fileName;
                ViewBag.fileName = fileName;              

                var create = new Profile {
                    userName = profile.userName,
                    uId = profile.uId,
                    rId = profile.rId,
                    Mobile = profile.Mobile,
                    Job = profile.Job,
                    City = profile.City,
                    Address = profile.Address,
                    dof = profile.dof,
                    DP = profile.DP = Path.GetFileName(id+".png"),
                    CreatedOn = profile.CreatedOn,
                    Status = profile.Status
                }; 
                    _context.Add(profile);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));                
            }
            ViewData["rId"] = new SelectList(_context.Set<CharityRole>(), "rId", "Name", profile.rId);
            return View(profile);
        }

问题:如何删除现有图片并添加一个新的同名图片,其在用户个人资料上的显示方式如下:

<img src="@Url.Content("~/Upload/DP/"+ _userManager.GetUserName(User)+".png")">

1 个答案:

答案 0 :(得分:1)

您的帖子中有两个问题。

  • 您需要对Using使用FileStream,否则将不处理文件流。

            using (var fileStream = new FileStream(fileName, FileMode.Create))
            {
                await DP.CopyToAsync(fileStream);
            }
    
  • 您需要将create而不是profile传递给_context.Add

这是完整的演示代码:

        public async Task<IActionResult> Create(Profile profile, IFormFile DP)
    {
        if (ModelState.IsValid)
        {
            var id = _userManager.GetUserName(HttpContext.User);
            var fileName = Path.Combine(_environment.WebRootPath + "/Upload/DP/", Path.GetFileName(id + ".png"));
            using (var fileStream = new FileStream(fileName, FileMode.Create))
            {
                await DP.CopyToAsync(fileStream);
            }
            var create = new Profile
            {
                UserName = profile.UserName,                    
                DP = Path.GetFileName(id + ".png")                    
            };
            _context.Add(create);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(profile);
    }