Asp.net core mvc 如何在指定文件夹中上载图像并在数据库.NET core中保存路径

Asp.net core mvc 如何在指定文件夹中上载图像并在数据库.NET core中保存路径,asp.net-core-mvc,Asp.net Core Mvc,当我保存图像时,图像成功地保存在数据库中,但它采用完整的图像路径,如C:\Users\VIZO\Desktop\employee.jpg我不想这样,我需要保存图像路径,如~images\employee.jpg并保存在特定文件夹中,并且相同的路径应该保存在数据库中,另外,如果有人在保存正确路径后告诉我如何查看该图像。您需要创建并保存可在浏览器中工作的路径。像这样 [HttpPost] [ValidateAntiForgeryToken] public async

当我保存图像时,图像成功地保存在数据库中,但它采用完整的图像路径,如
C:\Users\VIZO\Desktop\employee.jpg
我不想这样,我需要保存图像路径,如
~images\employee.jpg
并保存在特定文件夹中,并且相同的路径应该保存在数据库中,另外,如果有人在保存正确路径后告诉我如何查看该图像。

您需要创建并保存可在浏览器中工作的路径。像这样

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task Create([Bind]Employee employee)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                foreach (var Image in files)
                {
                    if (Image != null && Image.Length > 0)
                    {
                        var file = Image;

                        var uploads = Path.Combine(_appEnvironment.WebRootPath, "uploads\\img");
                        if (file.Length > 0)
                        {
                            var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName);
                            using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
                            {
                                await file.CopyToAsync(fileStream);
                                employee.ImageName = fileName;
                            }

                        }
                    }
                }
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Edit", new { id = employee.Id,name=employee.FirstName});
             }
        else
        {
            var errors = ModelState.Values.SelectMany(v => v.Errors);
        }
            return View(employee);

        }
运行时创建的文件名

行动方法更新

"uploads/img/" + fileName

你能指导我如何在上面的代码中操作吗?
[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task Create([Bind]Employee employee)
    {
        string uploadPath = "uploads/img";
        if (ModelState.IsValid)
        {
            var files = HttpContext.Request.Form.Files;
            foreach (var file in files)
            {
                if (file != null && file.Length > 0)
                {
                    var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName);
                    var uploadPathWithfileName = Path.Combine(uploadPath, fileName);

                    var uploadAbsolutePath = Path.Combine(_appEnvironment.WebRootPath, uploadPathWithfileName);

                    using (var fileStream = new FileStream(uploadAbsolutePath, FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                            employee.ImageName = uploadPathWithfileName;
                        }
                }
            }
            db.Employees.Add(employee);
            db.SaveChanges();
            return RedirectToAction("Edit", new { id = employee.Id, name = employee.FirstName });
        }
        else
        {
            var errors = ModelState.Values.SelectMany(v => v.Errors);
        }
        return View(employee);

    }