views:

466

answers:

2

OK I've seen a few people with this issue - but I'm already using a file path, not a relative path. My code works fine on my PC, but when another developer goes to upload an image they get this error. I thought it was a security permission thing on the folder - but the system account has full access to the folder (though I get confused about how to test which account the application is running under). Also usually running locally doesn't often give you too many security issues :)

A code snippet:

        Guid fileIdentifier = Guid.NewGuid();
        CurrentUserInfo currentUser = CMSContext.CurrentUser;
        string identifier = currentUser.UserName.Replace(" ", "") + "_" + fileIdentifier.ToString();
        string fileName1 = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName);
        string Name = System.IO.Path.GetFileNameWithoutExtension(fileName1);
        string renamedFile = fileName1.Replace(Name, identifier);
        string path = ConfigurationManager.AppSettings["MemberPhotoRepository"];

        String filenameToWriteTo = path + currentUser.UserName.Replace(" ", "") + fileName1;
        fileUpload.PostedFile.SaveAs(filenameToWriteTo);

Where my config settings value is:

Again this works fine on my PC! (And I checked the other person has the folder on their PC).

Any suggestions would be appreciated - thanks :)

A: 

Obviously what it says is that your filenameToWriteTo is not a rooted path... ie it is a relative path, and your server configuration doesn't like that. Use Server.MapPath to obtain real paths... something like...

string realPhysicalPath = Path.Combine(Server.MapPath(" "), filenameToWriteTo);
fileUpload.PostedFile.SaveAs(realPhysicalPath);

Just in case print out your filenameToWriteTo to see if its relative or physical path. If it's relative use the approach above.

m0s
Thanks - but my configuration value was a physical file path - so it was a real path. Sorry had a copy paste fail so it wouldn't have been obvious that it contained a physical file path :)
Jen
A: 

Not sure what the issue was as it sorted itself out. We didn't end up debugging it as it was on a non developer's PC and I had a workaround for him to continue his work whilst I investigated potential causes. So maybe was the way he updated/ran the site or file permissions fixed by grabbing a copy of my directory.

At least I know it needs physical file paths so hopefully won't come across this again!

Jen