【问题标题】:Upload file: Access denied上传文件:拒绝访问
【发布时间】:2013-08-08 15:49:33
【问题描述】:

我有一个 ASP.NET Web 表单网站。当我按下上传按钮时,此代码运行。这段代码有很多问题。它不是快速和简单的,它有一个大问题。第一次上传图片,效果很好,但是当我想替换文件时返回Access denied 错误。我在 IIS express 上遇到此错误,但它在 Visual Studio Development Server 上正常工作。


注意我有一个网格视图,显示必须更改哪个用户图像;这个网格视图还可以帮助我找到特定的用户图像文件。

   if (UsersImageFileUpload.HasFile && UsersImageFileUpload.PostedFile.ContentType.StartsWith("image"))
    {
        string filename = UploadUsersImageGridView.SelectedRow.Cells[1].Text.ToString();
        if (!File.Exists(Server.MapPath("~/App_Data/Backups/" + filename)))
        {
            UsersImageFileUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data/Users/") + filename);
        }
        else if (File.Exists(Server.MapPath("~/App_Data/Backups/" + filename)))
        {
            File.Delete(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
            UsersImageFileUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
            File.Replace(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"), Server.MapPath("~/App_Data/Users/") + filename, Server.MapPath("~/App_Data/Backups/"));
            File.Delete(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
        }
    }
}

错误标题Server Error in '/' Application

错误第二个标题The process cannot access the file 'D:\Website\App_Data\Users\1-fullname' because it is being used by another process.

错误说明An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

错误详情System.IO.IOException: The process cannot access the file 'D:\App_Data\Users\1-fullname' because it is being used by another process


我的问题:

  • 我的代码有什么问题?
  • 为什么 IIS 返回该错误(拒绝访问文件)?
  • IIS Express 8 和 Visual Studio 2012 开发服务器之间最重要的区别是什么?
  • 有更快的创建上传和替换的方法吗?
  • 有没有更好的形式来拥有这个系统?

【问题讨论】:

  • 运行应用程序池的帐户需要具有读取/写入该文件的权限 - 这是我首先要看的地方。
  • 检查您是否有权访问临时 Internet 文件位置
  • 此代码在localhost 上也有问题。这不仅仅是我服务器的问题。
  • 这不是权限问题。您要使用的文件在您要访问时正在使用中。找到正在使用该文件的进程并将其杀死。该主题可能对您有所帮助:stackoverflow.com/questions/317071/…

标签: asp.net


【解决方案1】:

1)你的代码是正确的

2)可能是因为你要替换的文件设置为“只读”,改一下试试替换

3)不知道

4)您可以尝试任何自定义上传方式或使用ajax,它会更快但不像asp.net文件上传那样简单

5)我觉得这个不错

【讨论】:

  • 该网站在许多服务器上为许多客户使用,我无法更改这些服务器的配置和设置。您知道有什么方法可以更改我的网站设置并在具有不同设置的许多服务器上使用它吗?
  • 有什么方法可以改变 IIS Express 的设置?
  • 我自己的回答完成了你的回答。
  • 嗨...感谢您的关注。请删除您的答案,因为我想删除我的问题。谢谢!
【解决方案2】:

试试这个:- 右键单击放置文件的文件夹并授予完全访问权限

【讨论】:

  • 有什么解决方案可以从我的网站更改访问选项?文件夹是 Everyone完全权限。你的回答错了!
  • 试过了。对不起,但没有工作。 这是从服务器 (Windows) 更改访问和权限的好方法,但我也需要从我的网站获取的方法。 由于文件正在使用中,因此无法访问该文件。
  • 更改IIS绑定的端口号可能是另一个应用程序使用相同的端口号
  • 帮不上忙。问题不在这里。它在开发服务器上运行良好。问题不仅仅来自我的 Windows IIS。当我在 WebMatrix (IIS Express 8) 上测试它时也有这个问题。
  • 您的文件已被使用并在另一个进程中打开,因此请在任务管理器中检查 IIS 重启回收并访问此网址可能会对您有所帮助:-codeproject.com/Questions/295198/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 2015-03-19
  • 2019-09-18
  • 1970-01-01
相关资源
最近更新 更多