Question

In my website I save images to a folder(Photos) in code-behind by calling

ImageUploadControl.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("~/Photos/" + Name + ext));

which throws an error 'Access not allowed' in medium trust. Any alternate to save images in medium trust? The folder needs to be publicly accessible as clients access these images without any authentication, so I cannot save it in App_Data(one fix to this problem)

*Website is hosted on hostgator shared plan. There is a limitation to how much I can ask for server configurations.

EDIT:

Yes its a access issue, as resolving the ~ tilda, virtual paths is not allowed. I hope anyone has a workaround for this problem.

Was it helpful?

Solution

Given you're using a native .NET control, I think it's more due to file system permissions than the trust of the environment. If you were using a 3rd party control I'd say then it's a trust issue.

Having a quick look at Hostgator they use cPanel, from there there's the file manager section, and in here if you go and select the 'Photos' folder and select "Change Permissions", here make sure the folder has write access from the application.

Edit


Support article here for setting *nix permissions: https://support.hostgator.com/articles/cpanel/how-to-change-permissions-chmod-of-a-file

And: https://support.hostgator.com/articles/specialized-help/technical/my-script-needs-to-use-777-permissions

OTHER TIPS

OK, as it's a path issue:

https://support.hostgator.com/articles/hosting-guide/lets-get-started/server-path-absolute-path

Is it running ASP.NET on a *nix box? Or is it running on a Windows Server and IIS?

Also, I think you may need to revise your code in this instance:

string pathRoot = HttpContext.Current.Server.MapPath("~/Photos/");
ImageUploadControl.PostedFile.SaveAs(pathRoot + "/" + Name + ext);

It looks like your original code is trying to use mapPath with the non-existing file path included.

This may work if you must have it one line:

ImageUploadControl.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("~/Photos/") + Name + ext);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top