【问题标题】:how to save a file to upper level folder?如何将文件保存到上级文件夹?
【发布时间】:2012-10-09 09:44:44
【问题描述】:

服务器中的文件夹结构如下:

  • www.example.com
    • 图片
    • 网络文件
      • 这里所有的网络文件
      • index.html
      • example.html

我的代码在网络文件夹中。要将文件保存在网络文件中,我可以使用server.MapPath("~\\folder")
但是如何将文件保存到位于 www.example.com 下的 web 文件的上层和相同 lvl 的 images 文件夹中?

新手,请指导..

【问题讨论】:

  • 我认为他的意思是文件夹在根目录之上...
  • 在您的 web.config 中添加文件夹路径并将文件保存在该路径中。很简单。
  • @codingkiwi.com 是的,我正在尝试将文件保存到应用程序根文件夹上方的文件夹中。

标签: c# webforms directory


【解决方案1】:

怎么样?

server.MapPath("~/images/myfile.jpg"); 

【讨论】:

  • 只会将文件保存到web file目录。
  • images 文件夹和 web 文件夹在同一级别。保存文件的代码在 web 文件夹中。所以如果使用此代码server.mappath("~\\myfile.jpg");,它只能将文件保存在web文件夹中。
  • 将 images 文件夹指定为 iis 中的虚拟目录,因此对于代码,它的行为就像在 web 文件夹中一样,但实际上它将在您现在拥有的结构中。见technet.microsoft.com/nl-nl/library/cc771804(v=ws.10).aspx
【解决方案2】:

MapPath 方法将指定路径映射到物理路径。

这是给你的一些注意事项

    <%
response.write(Server.MapPath("test.asp") & "<br />")
response.write(Server.MapPath("script/test.asp") & "<br />")
response.write(Server.MapPath("/script/test.asp") & "<br />")
response.write(Server.MapPath("\script") & "<br />")
response.write(Server.MapPath("/") & "<br />")
response.write(Server.MapPath("\") & "<br />")
%>

Output:

c:\inetpub\wwwroot\script\test.asp
c:\inetpub\wwwroot\script\script\test.asp
c:\inetpub\wwwroot\script\test.asp
c:\inetpub\wwwroot\script
c:\inetpub\wwwroot
c:\inetpub\wwwroot 

如果你想使用你的 www 或根目录的相对路径

 response.write(Server.MapPath("../"))

 response.write(Server.MapPath("..\"))

【讨论】:

  • 我试过了,但只能保存在应用程序文件夹中。我正在尝试将文件保存在应用程序根目录之上。 Server.MapPath("..\")(Server.MapPath("../") 给我一个错误无法使用前导 .. 退出顶级目录。
猜你喜欢
  • 1970-01-01
  • 2015-04-28
  • 2022-01-06
  • 2013-09-03
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2015-09-18
相关资源
最近更新 更多