繁体   English   中英

保存到文件路径时访问被拒绝

[英]Access denied when saving to file path

我有一个看起来像这样的上传方法

public ActionResult UploadImage(int? id)
{
    if (id == null)
        return HttpNotFound();

    Component c = db.Components.Find((int)id);
    HttpPostedFileBase photo = Request.Files["image"];

    if (photo != null && photo.ContentLength > 0)
    {
        var file = IGT.imagePath + "\\Components\\";

        //bool exists = System.IO.Directory.Exists(Server.MapPath(file));

        //if (!exists)
        //   System.IO.Directory.CreateDirectory(Server.MapPath(file));

        var filename = file + id.ToString() + ".jpg";

        if (!System.IO.Directory.Exists(file))
        {
            System.IO.Directory.CreateDirectory(file);
        }

        photo.SaveAs(filename);
        c.Image_Url = IGT.baseUrl + "/Content/images/Components/" + id.ToString() +".jpg";
        db.SaveChanges();
    }

但我在photo.SaveAs(filename);收到错误

System.UnauthorizedAccessException:“拒绝访问路径 'C:\Users\chris\Source\Repos\inventory2.0\PIC_Program_1.0\Content\images\Components\498.jpg'。”

为什么会这样,我该如何解决?

无论用户正在执行该代码,都无权写入该文件路径。 如果您从 go 到 C:\Users\chris\Source\Repos\inventory2.0\PIC_Program_1.0\Content\images\Components,右键单击,属性,安全选项卡,您将看到有权限的用户以及这些权限是什么. 您可以在那里添加或编辑您的用户权限。

我认为问题是您的应用程序用户无权访问您的文件夹。 如果您在 VS IIS express 中对此进行测试,那么您应该为当前用户授予权限。

但是,如果您从 IIS 服务器收到此错误消息。 然后您应该授予应用程序池标识(IIS Apppool\apppoolname)的权限。

进程监视器可以帮助您始终修复拒绝访问错误。 您只需要为 Result ="access is denied" 创建一个过滤器。 然后它会告诉你需要谁和什么许可。

https://docs.microsoft.com/en-us/sysinternals/downloads/procmon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM