C# ASP.net 收到错误“对路径的访问被拒绝”。在尝试将文件上传到我的 Windows Server 2008 R2 Web 服务器时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19724297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:44:54  来源:igfitidea点击:

ASP.net Getting the error "Access to the path is denied." while trying to upload files to my Windows Server 2008 R2 Web server

c#asp.net.netasp.net-4.0windows-server-2008-r2

提问by Hassan Mokdad

I have an asp.net webapplication that uploads files to a specific folder on the Web server. locally everything works fine, but when I deploy the application to the Webserver, I begin getting the error "Access to the path "D:\Attachments\myfile.doc" is denied". I gave the "IIS AppPool" user that the application is running under full permission on the folder. I even gave "Everyone" full permissions, but with the same error.

我有一个 asp.net web 应用程序,可以将文件上传到 Web 服务器上的特定文件夹。本地一切正常,但是当我将应用程序部署到 Web 服务器时,我开始收到错误“访问路径“D:\Attachments\myfile.doc”被拒绝”。我向“IIS AppPool”用户提供了该应用程序在该文件夹的完全权限下运行的信息。我什至给了“所有人”完全权限,但出现了同样的错误。

I added the folder to the Exceptions list of the Antivirus, but with the same result. I am begining to suspect that maybe Windows Server 2008 R2 needs a trick for my upload to work. I really appreciate your help.

我将该文件夹添加到防病毒软件的例外列表中,但结果相同。我开始怀疑 Windows Server 2008 R2 可能需要一个技巧才能让我的上传工作。我真的很感谢你的帮助。

Thanks

谢谢

采纳答案by Damian Drygiel

Your asp.net account {MACHINE}\ASPNETdoes not have write access to that location. That is the reason why its failing.

您的 asp.net 帐户{MACHINE}\ASPNET没有对该位置的写访问权限。这就是它失败的原因。

Consider granting access rights to the resource to the ASP.NET request identity.

考虑向 ASP.NET 请求标识授予对资源的访问权限。

Right click on downloading folder Properties > Security Tab > Edit > Add > locations > choose your local machine > click OK > Type ASPNET below "Enter the object name to select" > Click Check NamesCheck the boxes for the desired access (Full Control). If it will not work for you do the same with Network Service

右键单击下载文件夹Properties > Security Tab > Edit > Add > locations > choose your local machine > click OK > Type ASPNET below "Enter the object name to select" > Click Check Names选中所需访问权限的框 ( Full Control)。如果它对您不起作用,请执行相同的操作Network Service

Now this should show your local {MACHINENAME}\ASPNETaccount, then you set the write permission to this account.

现在这应该显示您的本地{MACHINENAME}\ASPNET帐户,然后您为此帐户设置写权限。

Otherwise if the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

否则,如果应用程序通过 模拟<identity impersonate="true"/>,则身份将是匿名用户(通常为IUSR_MACHINENAME)或经过身份验证的请求用户。



Or just use dedicated location for storing files in ASP.NET which is App_Data. To create it right click on your ASP.NET Project (in Visual Studio) Add > Add ASP.NET Folder > App_Data. Then you'll be able to save data to this location:

或者只是使用专用位置在 ASP.NET 中存储文件,即App_Data. 要创建它,请右键单击您的 ASP.NET 项目(在 Visual Studio 中)Add > Add ASP.NET Folder > App_Data。然后你就可以将数据保存到这个位置:

var path = Server.MapPath("~/App_Data/file.txt");
System.IO.File.WriteAllText(path, "Hello World");

回答by Ivan Sander de Jong

the problem might be that networkservice has no read rights

问题可能是网络服务没有读取权限

salution:

敬礼:

rightclick your upload folder -> poperty's -> security ->Edit -> add -> type :NETWORK SERVICE -> check box full control allow-> press ok or apply

右键单击您的上传文件夹 -> poperty 的 -> 安全性 -> 编辑 -> 添加 -> 键入:NETWORK SERVICE -> 复选框完全控制允许 -> 按确定或应用

回答by UMAR

Right click on your folder on your server or local machine and give full permissions to

右键单击服务器或本地计算机上的文件夹并授予完全权限

IIS_IUSRS

IIS_IUSRS

that's it.

就是这样。

回答by Shaakir

If anyone stumbles across this as it is the first result in google,

如果有人偶然发现这是谷歌的第一个结果,

remember to specify the filename too in the SaveAs method.

记得在 SaveAs 方法中也指定文件名。

Won't work

行不通

file_upload.PostedFile.SaveAs(Server.MapPath(SaveLocation));

You need this:

你需要这个:

filename = Path.GetFileName(file_upload.PostedFile.FileName);
file_upload.PostedFile.SaveAs(Server.MapPath(SaveLocation + "\" + filename));

I assumed the SaveAs method will automatically use the filename uploaded. Kept getting "Access denied" error. Not very descriptive of the actual problem

我假设 SaveAs 方法会自动使用上传的文件名。一直收到“拒绝访问”错误。没有很好地描述实际问题

回答by Adhik

Have you looked under Advanced Security Settings?

您是否查看了高级安全设置?

something like below image change permissions of folder to IIS_IUSRS

类似于下面的图像将文件夹的权限更改为 IIS_IUSRS

enter image description here

enter image description here

回答by Adhik

I faced this problem once and in my case the soln is to close any process in the task manager that uses/accesses that file.

我曾经遇到过这个问题,在我的情况下,解决方法是关闭任务管理器中使用/访问该文件的任何进程

回答by Lalit Khanna

Go to root folder

转到根文件夹

Right Click, click on Properties

右击,点击属性

Choose Tab Security

选择标签安全

Click on Edit

点击编辑

Click on Add

点击添加

Type 'EveryOne'

输入“所有人”

Click OK

单击确定

Check Out Full Control

检查完全控制

Click OK

单击确定

回答by Spencer Sullivan

I know this is late to the game, but I wanted to share in case it helps someone.

我知道这已经晚了,但我想分享一下,以防它对某人有所帮助。

Your exact situation may not apply, however I had a similar situation and setting the File attribute helped.

您的确切情况可能不适用,但是我遇到了类似的情况并且设置 File 属性有帮助。

Try to set the File attribute to Normal:

尝试将 File 属性设置为 Normal:

var path = Server.MapPath("~/App_Data/file.txt");
File.SetAttributes(path, FileAttributes.Normal);
System.IO.File.WriteAllText(path, "Hello World");

I hope this helps someone.

我希望这可以帮助别人。