无法在网络服务器上保存文件:System.UnauthorizedAccessException
我无法使用 FileUpload.SaveAs() 或 File.WriteAllText() 在网络服务器上保存文件。 我保存的文件夹是 CHMOD 666。
我的代码是这样的:
File.WriteAllText(MapPath(".") + "\\Ads\\test.jpg", "test");
这段代码在 Dev 中完美运行。 服务器。
此致, Lasse Espeholt
System.UnauthorizedAccessException:对路径 *' 的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 errorCode, String MaybeFullPath) 在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy) 在 System.IO.FileStream..ctor(字符串路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、FileOptions 选项、String msgPath、布尔值 bFromProxy) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式) 在 System.Web.HttpPostedFile.SaveAs(字符串文件名) 在 System.Web.UI.WebControls.FileUpload.SaveAs(字符串文件名) 在*(对象发送者,ListViewInsertEventArgs e)
I can't save a file on my webserver with FileUpload.SaveAs() or with File.WriteAllText(). The folder I save in is CHMOD 666.
My code is this:
File.WriteAllText(MapPath(".") + "\\Ads\\test.jpg", "test");
This code works perfectly in Dev. server.
Best regards,
Lasse Espeholt
System.UnauthorizedAccessException: Access to the path *' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at System.Web.HttpPostedFile.SaveAs(String filename)
at System.Web.UI.WebControls.FileUpload.SaveAs(String filename)
at *(Object sender, ListViewInsertEventArgs e)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要向计算机的 ASPNET 帐户授予所需文件夹的读写权限。
您可以通过打开文件夹属性 -> 来完成此操作。 安全选项卡。
You will need to give read write permissions to machine's ASPNET account for desired folder.
You can do it by opening folder properties -> security tab.
您的错误消息指出:
这看起来有点奇怪。 您是否编辑了路径,或者它确实是这样显示的? 如果是后者,那么您对 MapPath 的调用就会出现问题。
我知道以下内容可能看起来很明显,但过去我已经多次被假设所困扰。
您是否已验证您认为要写入的文件夹就是
MapPath(".") + "\\Ads\\test.jpg"
实际上解析为?您是否也拥有“广告”子文件夹的完整权限?
Your error message states:
This looks a little odd. Have you edited the path or is that exactly how it's displayed? If the latter then something is going wrong with your call to
MapPath
.I know the following may seem obvious, but I've been bitten by assumptions too many times in the past.
Have you verified that the folder you think you're writing to is the one that
MapPath(".") + "\\Ads\\test.jpg"
actually resolves to?Do you have full rights to the "Ads" sub folder as well?