1
[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.

1 Answer 1

1

You need to create and save that path which can work in browser. like this

"uploads/img/" + fileName

fileName which you create at run time.

Action Method Updated

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

    }
Sign up to request clarification or add additional context in comments.

1 Comment

can you guide me little bit how i can do in my code above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.