Search code examples
c#asp.net-mvcsave-asserver.mappath

How to give path of a folder in server to save an image?


I'm trying to store an image into two applications (both published on server). This is my code to save the image :

 string path = Server.MapPath("/Images/Landing/bottom_banner/");           
 string path1 = @"_http://10.241.193.22/Myapplication/Images/Landing/bottom_banner/";
  HttpPostedFileBase photo = Request.Files["adup"];
            if (photo.ContentLength != 0)
            {
                string lastPart = photo.FileName.Split('\\').Last();
                Random random = new Random();
                int rno = random.Next(0, 1000);
                photo.SaveAs(path + rno + lastPart);
                photo.SaveAs(path1 + rno + lastPart);
            }

Note: Myapplication is another application hosted on the same server

My problem is I am able to save the image in my first application using Server.MapPath but when the compiler comes to the part photo.SaveAs(path1 + rno + lastPart) it gives an error:

The SaveAs method is configured to require a rooted path, and the path '_http://10.241.193.22/Myapplication/Images/Landing/bottom_banner/676Chrysanthemum.jpg' is not rooted

Please suggest how can I eliminate this issue?


Solution

  • I am not sure whether this is right but can you do this ?

    In the current application store the Server.MapPath value and then replace the current application name with "Myapplication" and then add the trailing path. Something like this

    string path1 = Server.MapPath("");
    path1.Replace("Application1", "Myapplication"); //Considering "Application1" is the name of your current application
    path1 += "/Images/Landing/bottom_banner/";
    HttpPostedFileBase photo = Request.Files["adup"];
            if (photo.ContentLength != 0)
            {
                string lastPart = photo.FileName.Split('\\').Last();
                Random random = new Random();
                int rno = random.Next(0, 1000);
                photo.SaveAs(path1 + rno + lastPart);
            }
    

    There might be a permission problem with this one. I have not checked it. If it works please let me know.