Muhammad Imran Khan
Muhammad Imran Khan

Reputation: 325

Cannot display images using path created Server.MapPath

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.

Upvotes: 1

Views: 2543

Answers (2)

user174806
user174806

Reputation: 1

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()" />

Upvotes: 0

Shaiju T
Shaiju T

Reputation: 6609

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

Upvotes: 1

Related Questions