none
How to Upload image in specified folder and save path in database .NET core RRS feed

  • Question

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

    when i save image, image save successfully in database, but it takes full image path like this C:\Users\VIZO\Desktop\employee.jpg i dont want like this, i need to save image path somehting like this ~images\employee.jpg and in specific folder and same path should save in database, also if someone show me after saving correct path how i can view that image.

    Thursday, February 22, 2018 9:20 PM

All replies

  • https://forums.asp.net/1255.aspx/1?ASP+NET+Core
    Thursday, February 22, 2018 10:54 PM
  • Hi veezo,

    I agree with DA924x.

    Since your issue is related to ASP.NET core, you can post the issue on above forum.

    If you want to upload image using ASP.NET core, Here is a similar solution,  Upload Image in Asp.Net Core?

    Best Regards,

    Hart


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, February 25, 2018 4:51 AM