1

I am trying display images on the webpage from the folder placed in my project root directory. The path that I am storing in the database is as follows:

D:\Projects\OnlineStore\OnlineStore\OnlineStore\Content\Uploads\Images\Bundles\706976d31e274e7ab36986b9bec2f0f9-Object Image.jpg

The code that generated this path is as follows:

var path = Path.Combine(Server.MapPath("~/Content/Uploads/Images/Bundles"), fileId);
photo.SaveAs(path);

Image doesn't show using this path. The path that works is as follows:

\Content\Uploads\Images\Bundles\706976d31e274e7ab36986b9bec2f0f9-Object Image.jpg

How do I resolve this issue? I was thinking about using first path to save image file to folder and save second path in the database. But this doesn't seem the right way of doing this.

8
  • Image is within another assembly (dll)? Commented Jan 15, 2018 at 7:29
  • Have you tried var path = Path.Combine(System.Web.Host.HostingEnvironment.MapPath("~/Content/Uploads/Images/Bundles"), fileId); Commented Jan 15, 2018 at 7:39
  • Can you post your view code ? , so that others can guide better. Commented Jan 15, 2018 at 8:06
  • 1
    just store image name inly in database as : 706976d31e274e7ab36986b9bec2f0f9-Object Image.jpg and display the image using any path you like Commented Jan 15, 2018 at 8:14
  • Hi Valkyrie! Image is placed under the root directory, so yes i guess. Commented Jan 15, 2018 at 8:44

2 Answers 2

1

1. Only store FileName in database Check this.

  string fileName = System.IO.Path.GetFileName(file.FileName);

 //store fileName in your ImageName column of Image your Image table
 //Note: generate unique filename using `Guid` or `PrimaryKey` to overcome    
 //same file name issue.

2. Use @Url.Content to show image in view.

<img  src="@Url.Content("~")/Content/Uploads/Images/Bundles/@Model.ImageName"/>

Reference

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

Comments

0

In controller:

public ActionResult UserRegister(Register Register)
    {
        try
        {
            DbConnection dbHandle = new DbConnection();
            dbHandle.Connection();

             using (SqlCommand UserRegistercmd = new SqlCommand("USPUserRegistration", dbHandle.con))
                {
                    DateTime dob = Convert.ToDateTime(Register.dateOfBirth);
                    string Random = System.DateTime.Now.ToString("ddMMyyhhmmss");
                    Register.UserPhoto = "../Images/" + Random + Register.userImg.FileName;
                    Register.userImg.SaveAs(Server.MapPath("../Images/") + Random + Register.userImg.FileName);
                    UserRegistercmd.CommandType = CommandType.StoredProcedure;

                    dbHandle.con.Open();
                    UserRegistercmd.ExecuteNonQuery();
                    dbHandle.con.Close();
                    ViewBag.error = "Company Registration Sucess";

                    Mail.SendMail(Register.email,"Your User Name and Password ","User Name :"+Register.username+"Paassword :"+Register.password);
                }
        }
        catch (Exception e)
        {
            ViewBag.error = "Error!!";
            ExceptionLog.Log(e, Request.UserHostAddress);
            return RedirectToAction("Error_View", "CompanyRegister");
        }
        finally
        {
            Dispose();
        }
        return RedirectToAction();
    }

in cshtml use @Url.Content:

  <img src="@Url.Content(@Model.UserPhoto)" alt="There is no Image" style="height:150px;width:150px" onclick="ChangeImg()" />

Comments

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.