Search code examples
c#file-uploadsave-as

The SaveAs method is configured to require a rooted path, and the path <blah> is not rooted


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 :)


Solution

  • 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!