Server.MapPath - Not allowed to load local resource error

13,842

Solution 1

You can try this

string path = Server.MapPath("~/Uploads/User_" + userId + "/");

Map path need Virtual path so "~/" will surely fix it.

Solution 2

For me removing Server.MapPath worked.. I use tild ('~') sign with the url and it worked perfectly.

string path = "~/Uploads/User_" + userId + "/";
Share:
13,842
1110
Author by

1110

Updated on June 04, 2022

Comments

  • 1110
    1110 almost 2 years

    I use Server.MapPath to access some images on server. And I got the right path but can't open it. The error I got is:

    Not allowed to load local resource: d:/Project/MyProject/WebUI/Uploads/User_18234/leopard.jpg
    

    I suppose that this will work on server. But I need it on localhost to test. Is there any solution for this?

    ...
    userId = GetUserId();
    string path = Server.MapPath("Uploads/User_" + userId + "/");
    model.Name = path + model.Name + "_Original." + model.Extension;
    ...
    
  • 1110
    1110 over 12 years
    No it doesn't work like that. But it works when I use "../../../../Uploads/User...".
  • Ankit Patial
    Ankit Patial over 12 years
    yes you are correct, it means the virtual path you need is "~/../../../Uploads/User...". Path must be with respect to project root.
  • 1110
    1110 over 12 years
    This means that when I write "~/../../../Something" this actually is relative path. Am I right?
  • Ankit Patial
    Ankit Patial over 12 years
    if possible do use the Absolute path e.g. "~/<dir>/<dir>/<file>" better approach than the relative path e.g "../../<file>". Reason is that "../" varies w.r.t caller(page) depth