开发者

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

开发者 https://www.devze.com 2022-12-30 11:26 出处:网络
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

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


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.


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!


Please, make sure that folder where you are saving the file exists. If folder does not exist, it will not be created for you and SaveAs method will throw exception you are having now.

Thank you.

0

精彩评论

暂无评论...
验证码 换一张
取 消