【问题标题】:Can't upload image to site on IE8: System.UnauthorizedAccessException无法在 IE8 上将图像上传到站点:System.UnauthorizedAccessException
【发布时间】:2013-09-12 20:09:12
【问题描述】:

我正在尝试使用以下代码将图像上传到 IIS 6(Windows 2003 Server)站点:

[HttpPost]
    public ActionResult Edit(Empresas empresas)
    {
        Empresas e = db.Empresas.Where(em => em.Id == empresas.Id).First();
        e.NombreEmpresa = empresas.NombreEmpresa;
        HttpPostedFileBase archivoBanner = Request.Files["Banner"];
        HttpPostedFileBase archivoLogo = Request.Files["Logo"];

        string directorioUpload = Server.MapPath("~/Images/" + e.CodigoEmpresa);
        if (!Directory.Exists(directorioUpload))
        {
            Directory.CreateDirectory(directorioUpload);
        }

        if (archivoBanner != null)
        {
            if (archivoBanner.ContentLength > 0)
            {
                var fileUpload = Path.Combine(directorioUpload, archivoBanner.FileName);
                archivoBanner.SaveAs(fileUpload);
                e.Banner = archivoBanner.FileName;
            }
        }

        if (archivoLogo != null)
        {
            if (archivoLogo.ContentLength > 0)
            {
                var fileUpload = Path.Combine(directorioUpload, archivoLogo.FileName);
                archivoLogo.SaveAs(fileUpload);
                e.Logo = archivoLogo.FileName;
            }
        }

        if (ModelState.IsValid)
        {
            db.Entry(e).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(e);
    }

如果我从我的 PC(Windows 8、IE10、Chrome 29)或使用 Chrome 的服务器加载它,它没有问题。如果我从服务器加载它,使用 IE8 它会在“archivoBanner.SaveAs(fileUpload);”上抛出 System.UnauthorizedAccessException,说应用程序无法读取源图像:

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.UnauthorizedAccessException:Acceso denegado a la ruta de acceso 'C:\Documents and Settings\user\My 文档\Imagenes\banner.png'。

ASP.NET 无权访问请求的资源。考虑 向 ASP.NET 请求授予对资源的访问权限 身份。 ASP.NET 有一个基本进程标识(通常 IIS 5 上的 {MACHINE}\ASPNET 或 IIS 6 和 IIS 7 上的网络服务,以及 IIS 7.5 上配置的应用程序池标识),如果 该应用程序不是模拟的。如果应用程序是 冒充 via ,身份将是 匿名用户(通常是 IUSR_MACHINENAME)或经过身份验证的用户 请求用户。

要授予 ASP.NET 对文件的访问权限,请在资源管理器中右键单击该文件, 选择“属性”并选择“安全”选项卡。点击“添加”添加 适当的用户或组。突出显示 ASP.NET 帐户,然后 选中所需访问权限的复选框。

来源错误:

在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

堆栈跟踪:

[UnauthorizedAccessException: Acceso denegado a la ruta de acceso 'C:\Documents and Settings\user\My Documents\Imagenes\banner.png'.] System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +12898791 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,FileOptions 选项,SECURITY_ATTRIBUTES secAttrs, 字符串 msgPath,布尔 bFromProxy,布尔 useLongPath) +2481
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess 访问、FileShare 共享、Int32 bufferSize、FileOptions 选项、字符串 msgPath,布尔 bFromProxy)+229 System.IO.FileStream..ctor(字符串 路径,FileMode 模式)+106 System.Web.HttpPostedFile.SaveAs(String 文件名)+295
SistemaSolicitudes.Controllers.EmpresasController.Edit(Empresas empresas) 在 D:...\Controllers\EmpresasController.cs:73 lambda_method(闭包, ControllerBase, Object[]) +127
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext 控制器上下文,IDictionary2 parameters) +248
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 参数)+39
System.Web.Mvc.Async.c_DisplayClass39.b_33() +125 System.Web.Mvc.Async.c_DisplayClass4f.b_49() +452 System.Web.Mvc.Async.c_DisplayClass37.b_36(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.c_DisplayClass2a.b_20() +31 System.Web.Mvc.Async.c_DisplayClass25.b_22(IAsyncResult asyncResult) +230
System.Web.Mvc.c_DisplayClass1d.b_18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +20 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult 异步结果)+53
System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +20
System.Web.Mvc.c_DisplayClass8.b_3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +20
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375

我已尝试更改目标文件夹的权限,但正如我所说,写入文件没有错误,但读取它。因此,我尝试更改文件权限,结果相同。

你能帮帮我吗?

【问题讨论】:

  • 您是否尝试从用户文件夹之外的文件夹上传文件?例如,从 C:\
  • 是的,结果是一样的。

标签: c# entity-framework asp.net-mvc-4 iis internet-explorer-8


【解决方案1】:

我怀疑问题出在以下行:

var fileUpload = Path.Combine(directorioUpload, archivoBanner.FileName);

如果 archivoBanner.FileName 包含绝对路径,则 Path.Combine 将返回该绝对路径并忽略 directorioUpload 参数。

你可以试试这样的:

var fileUpload = Path.Combine(
            directorioUpload, 
            Path.GetFileName(archivoBanner.FileName)
            );

根据您对问题的描述,我怀疑archivoBanner.FileName 在失败的情况下包含绝对路径,并且仅在成功的情况下包含相对路径或文件名。您可以轻松验证这一点。

这不是原因。 archivoBanner.Filename 仅包含文件名,不包含其在磁盘中的路径。

错误消息表明您的代码正在尝试访问 C:\Documents and Settings\user\My Documents\Imagenes\banner.png,但未能成功。这看起来很像正在上传的文件的路径。

从我的桌面尝试使用名为“banner.png”的文件,值是...

这与您的说法一致,即您在从 PC 上传时没有问题(我认为这就是您所说的“您的桌面”)。尝试使用 IE8 从服务器上传时跟踪上传的文件名。

documentation for HttpPostedFile.FileName 表示它是“客户端上文件的完全限定名”。出于安全原因,我怀疑大多数浏览器不会发送完全限定名称,尤其是对于远程客户端。但是您对问题的描述表明 IE8 在本地客户端时会这样做。

【讨论】:

  • 不是这个原因。 archivoBanner.Filename 仅包含文件名,而不包含其在磁盘中的路径。从我的桌面尝试使用名为“banner.png”的文件,值为:directioUpload =“D:\\Desarrollo\\...\\Images\\117”,archivoBanner.FileName =“banner.png”,fileUpload = "D:\\Desarrollo\\...\\Images\\117\\banner.png"
  • 谢谢你,乔。你是对的。 Internet Explorer 8 发送的是完整路径而不是文件名。您的建议帮助我解决了冲突。
  • +1,我从没想过这么小的错误花了我一整天的时间来解决。谢谢你!
【解决方案2】:

嗯,我的第一个想法是,也许你应该尝试在你的路径中摆脱 ~。

不过,你也可以查看this guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2020-07-07
    • 2017-01-29
    • 2015-05-15
    • 1970-01-01
    相关资源
    最近更新 更多