无法在 MVC 中加载文件

发布于 2024-11-30 17:25:08 字数 580 浏览 0 评论 0原文

我有下一个代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-12-07 17:25:08

这是一个安全异常,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

雪落纷纷 2024-12-07 17:25:08

我假设您在本地计算机上使用 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.

故笙诉离歌 2024-12-07 17:25:08

感谢您的帮助,但真正的原因不是访问问题,正如您所说。
真正的原因是空的“”值。所以我重新编写了我的代码,例如:

var ordinaryPropertyValue = new Catalog.Core.Entities.OrdinaryPropertyValue();

var fileFile = Request.Files["File" + prop.Id];

if (fileFile == null)
    continue;

string pathFile = Server.MapPath("~/temp");

string filenameFile = Path.GetFileName(fileFile.FileName);

if (!string.IsNullOrEmpty(filenameFile))
{
    fileFile.SaveAs(Path.Combine(pathFile, filenameFile));

    ordinaryPropertyValue.Value = Path.Combine(pathFile, filenameFile);

    instance.SetPropertyValue(prop.Id, ordinaryPropertyValue);
}

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:

var ordinaryPropertyValue = new Catalog.Core.Entities.OrdinaryPropertyValue();

var fileFile = Request.Files["File" + prop.Id];

if (fileFile == null)
    continue;

string pathFile = Server.MapPath("~/temp");

string filenameFile = Path.GetFileName(fileFile.FileName);

if (!string.IsNullOrEmpty(filenameFile))
{
    fileFile.SaveAs(Path.Combine(pathFile, filenameFile));

    ordinaryPropertyValue.Value = Path.Combine(pathFile, filenameFile);

    instance.SetPropertyValue(prop.Id, ordinaryPropertyValue);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文