Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
[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);

        }


What I have tried:

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.
Posted
Updated 14-Apr-18 5:52am

1 solution

Hi friend use below function it may help you out :

public static string MapPathReverse(string fullServerPath)
    {            
        return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath,String.Empty);
    }


if it does not help just do let me know i will fix it.
 
Share this answer
 
Comments
mdshammi 28-Jun-23 3:44am    
how to upload image in another domain path like(https:abc.com/uploads) in asp net core web api
mdshammi 28-Jun-23 4:05am    
how to upload image in url path in asp net core web api

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900