Question

asp.net MVC saving relative path in database as

~/Content/Uploads/11thMay.jpg

not the full path as

D:/Projects/HRMV2/CubicHRMWeb/Content/Uploads/11thMay.jpg

I wanted to save relative path but in DB full path was being saved.

How can I do it? thanks.

Was it helpful?

Solution

If your question is "How to save as full path", please try this

string path = Path.Combine(Server.MapPath("~/Content/img/Company/LogoFiles"), filename);

If your question is "How to save as relative path", please try this

[HttpPost]
public ActionResult Save(HttpPostedFileBase myFile)
{
    string filename = myFile.FileName;
    string relativeFileName = "~/Content/Uploads/" + filename;// relative path

    return View();
}

OTHER TIPS

this code doesn't allow to save the relative path, when you try it, the totality of filename is setted and not just the filename.if you insert a breakpoint at this point, you must obtain a thing like

   "/content/Uploads/C:Document/Picture/father.jpeg" 

and not the relative path which you will want it like

  "/content/Uploads/father.jpeg" 

in order to save the relative path of a document , you ought to use the getfilename property of system.io as show in the below example.

I suppose that you want to do it in the post method of your controller which contains a model or httppostedfilebase in my example, i have a model with httppostedfilebase property named imagefichier, so you can get the document name by doing this

 var lefilename =   System.IO.Path.GetFileName(model.imagefichier.FileName);
//Suppose that you want to save it in your root web file
 //like ~/Images/Administrateur/leilename,you can do it like this
    string cheminbdd = "~/Images/Administrateur/" + lefilename;
  //it is cheminbdd that you can save in your database as your relative path
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top