【问题标题】:How can I use Server.MapPath to upload a file onto a server which is mapped on my computer如何使用 Server.MapPath 将文件上传到映射在我的计算机上的服务器
【发布时间】:2018-11-25 06:25:17
【问题描述】:

到目前为止,我知道如何使用以下代码将文件上传到我的解决方案中的文件夹。

string root = HttpContext.Current.Server.MapPath("~/upload");

如何将文件保存到不在解决方案中的其他位置,即映射到我的电脑的服务器位置。

string root = HttpContext.Current.Server.MapPath("/Z:/UploadFolder"); I have tried this but its not saving to the server so where I am going wrong?

【问题讨论】:

  • MapPath 只是将您的根站点的相对路径 ~\ 替换为该文件夹的本地计算机上正确的完整操作系统路径名。它与服务器上可用的共享名或映射驱动器无关
  • 我想你误解了 MapPath 函数的用途

标签: c# asp.net-mvc file-upload knockout.js server.mappath


【解决方案1】:

当你有一个相对路径并且想要使用你的项目的路径时,你应该使用MapPath。对于您不需要MapPath 的另一条路径。像这样使用它:

string root ="Z:\\UploadFolder";

【讨论】:

    【解决方案2】:

    您可以将 IIS 中的虚拟目录映射到要保存到的位置。例如,将 UploadFolder 的虚拟目录映射到 z:\uploadfolder。然后,这将起作用:

    string root = HttpContext.Current.Server.MapPath("~/upload");
    

    确保您正确设置权限。

    【讨论】:

      【解决方案3】:

      您的逻辑似乎令人困惑。使用 Server.MapPath - 返回对应于指定虚拟路径的物理文件路径。

      但是您在第二个语句中将物理位置传递给 Server.MapPath,这没有达到 Server.MapPath 的全部目的。

      string root = HttpContext.Current.Server.MapPath("/Z:/UploadFolder"); **INCORRECT**
      

      理想情况下,您需要创建一个映射到“/Z:/UploadFolder”的虚拟目录并将其命名为“upload”。

      string root = HttpContext.Current.Server.MapPath("~/upload");
      

      注意:您需要传递显式凭据才能从 ASP.NET 访问网络共享。建议的方法是使用身份模拟,一旦完成,使用相同的逻辑重试。

      <configuration>
        <system.web>
          <identity impersonate="true" />
        </system.web>
      </configuration>

      【讨论】:

        猜你喜欢
        • 2017-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多