无法在 MVC 中加载文件
我有下一个代码:
var ordinaryPropertyValue = new Catalog.Core.Entities.OrdinaryPropertyValue();
Environment.CurrentDirectory = System.IO.Path.GetTempPath();
var fileFile = Request.Files["File" + prop.Id];
if (fileFile == null) continue;
string pathFile = Environment.CurrentDirectory;
fileFile.SaveAs(pathFile);
ordinaryPropertyValue.Value = pathFile;
instance.SetPropertyValue(prop.Id, ordinaryPropertyValue);
但是我无法加载我的文件,因为下一个问题:
AccessException:访问路径“C:\Users\Michael\AppData\Local” \Temp' 被拒绝。
I have next code:
var ordinaryPropertyValue = new Catalog.Core.Entities.OrdinaryPropertyValue();
Environment.CurrentDirectory = System.IO.Path.GetTempPath();
var fileFile = Request.Files["File" + prop.Id];
if (fileFile == null) continue;
string pathFile = Environment.CurrentDirectory;
fileFile.SaveAs(pathFile);
ordinaryPropertyValue.Value = pathFile;
instance.SetPropertyValue(prop.Id, ordinaryPropertyValue);
But I can't load my file 'cos the next problem:
AccessException: Access to the path 'C:\Users\Michael\AppData\Local\Temp' is denied.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个安全异常,Web 应用程序的执行用户无权在目标路径上写入。
请注意,您正在尝试将文件保存到不带文件名的目录路径。
无论如何,你的代码一团糟,我个人不会不会分配/更改
Environment.CurrentDirectory
This is a security exception, executing user of your web application has no rights to write on the target path.
Notice that your are trying to save a file to a directory path without the file name.
Your code is a mess anyway, personally I would NOT assign/change the
Environment.CurrentDirectory
我假设您在本地计算机上使用 IIS,在这种情况下,您可能需要向运行 Web 应用程序的帐户授予对此目录的写入权限(使用 VS 的内置服务器在您的帐户下运行)。
如果您使用的是 IIS 7 +,则为“ApplicationPoolIdentity”,否则为“NETWORK SERVICE”
PS 我也不建议更改环境设置。
I'm assuming you are using IIS on your local machine, in which case you possibly need to grant write permission to this directory to the account that the web app is running under (using VS's built in server runs under your account).
If you are using IIS 7 + it's 'ApplicationPoolIdentity' otherwise it's 'NETWORK SERVICE'
P.S. I also don't recommend changing the environment settings.
感谢您的帮助,但真正的原因不是访问问题,正如您所说。
真正的原因是空的“”值。所以我重新编写了我的代码,例如:
Thanks for help, but the real cause was not the problem with access, as you have said.
The real reason was empty "" value. So i have remake my code such as: