SaveAs 方法被配置为需要根路径,并且该路径没有扎根
好的,我见过一些人遇到这个问题 - 但我已经使用文件路径,而不是相对路径。我的代码在我的电脑上运行良好,但是当其他开发人员上传图像时,他们会收到此错误。我认为这是文件夹上的安全权限问题 - 但系统帐户具有对该文件夹的完全访问权限(尽管我对如何测试应用程序在哪个帐户下运行感到困惑)。通常在本地运行通常不会给您带来太多安全问题:)
代码片段:
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);
我的配置设置值是:
这在我的电脑上再次运行得很好! (我检查了其他人的电脑上是否有该文件夹)。
任何建议将不胜感激 - 谢谢:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,它所说的是您的 filenameToWriteTo 不是根路径...即它是相对路径,而您的服务器配置不喜欢这样。使用 Server.MapPath 获取真实路径...类似...
以防万一打印出您的 filenameToWriteTo 以查看其相对路径或物理路径。如果是相对的,请使用上面的方法。
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...
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!
请确保保存文件的文件夹存在。如果文件夹不存在,则不会为您创建它,并且 SaveAs 方法将抛出您现在遇到的异常。
谢谢。
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.