【问题标题】:The SaveAs method is configured to require a rooted path, and the path 'fp' is not rootedSaveAs 方法配置为需要有根路径,并且路径 'fp' 没有根
【发布时间】:2010-11-15 10:53:04
【问题描述】:

我在 Asp.net 中进行图像上传,并在我的控制下提供以下代码:

    string st;
    st = tt.PostedFile.FileName;
    Int32 a;
    a = st.LastIndexOf("\\");
    string fn;
    fn = st.Substring(a + 1);
    string fp;
    fp = Server.MapPath(" ");
    fp = fp + "\\";
    fp = fp + fn;
    tt.PostedFile.SaveAs("fp");

但在上传或保存图像时,会出现错误消息,即 SaveAs 方法配置为需要 root 路径,并且路径 'fp' 没有 root。 所以请帮我看看有什么问题

【问题讨论】:

  • 出于兴趣,您为什么将可以轻松用 5 行表示的代码拆分为 11 行?为什么要声明一个变量并且只在下一行赋值?
  • 还可以查看 System.IO.Path 以了解文件名的操作。

标签: c# .net file


【解决方案1】:

我遇到了同样的问题。问题是您没有指定要保存文件的服务器的路径。这是一个可能更简单的答案:

string fileName = tt.PostedFile.FileName;
string savePath = Server.MapPath("Path/Of/The/Folder/Comes/Here/") + fileName);
tt.PostedFile.SaveAs(savePath);

【讨论】:

    【解决方案2】:

    使用Server.MapPath()

    fileUploader.SaveAs(Server.MapPath("~/Images/")+"file.jpg");
    

    【讨论】:

      【解决方案3】:

      我们不能使用“SaveAs”方法直接写入 FTP 服务器。 以上方法只支持本地路径和UNC路径。

      要将其保存到 FTP,请使用 FtpWebRequest 类。

      您将在 social.msdn 中相同类型的问题答案中获得完整的详细信息。

      请通过链接..您将能够解决问题..

      enter link description here

      --感谢 Jesse HouwingXPirit(MCC,合伙人,MVP)的回答

      【讨论】:

        【解决方案4】:

        在阅读问题的标题时,我认为您似乎在变量名周围加上了引号。不是真的相信是这样,我打开问题阅读它,但它真的是这样......

        【讨论】:

          【解决方案5】:

          我怀疑问题在于您使用的是字符串“fp”而不是变量fp。这是固定代码,也使(IMO)更具可读性:

          string filename = tt.PostedFile.FileName;
          int lastSlash = filename.LastIndexOf("\\");
          string trailingPath = filename.Substring(lastSlash + 1);
          string fullPath = Server.MapPath(" ") + "\\" + trailingPath;
          tt.PostedFile.SaveAs(fullPath);
          

          您还应该考虑将倒数第二行更改为:

          string fullPath = Path.Combine(Server.MapPath(" "), trailingPath);
          

          您可能还想考虑如果发布的文件在文件名中使用 / 而不是 \ 会发生什么...例如,如果它是从 Linux 发布的。实际上,您可以将前三行全部更改为:

          string trailingPath = Path.GetFileName(tt.PostedFile.FileName));
          

          结合这些,我们会得到:

          string trailingPath = Path.GetFileName(tt.PostedFile.FileName));
          string fullPath = Path.Combine(Server.MapPath(" "), trailingPath);
          tt.PostedFile.SaveAs(fullPath);
          

          更干净,IMO :)

          【讨论】:

          • 谢谢先生,现在没有错误,但是当我调试图像时,Gridview 中没有显示。
          • 在不知道自己在做什么的情况下,很难为您提供帮助。我建议您再问一个问题,了解更多信息。
          • 先生,我在设计视图中有网格视图,其中我有 2 个列名称、类、图像。因此,对于图像,我在 Edititemtemplate 和 fottertemplate 中都有文件上传器。但是先生,当我上传任何图像时没有错误但图像没有显示,更新时同样的问题。我提供图像源(项目模板)此代码 - 但是在上传图像后保存在我系统的网站文件夹中。但是为什么它没有在浏览器中显示。请告诉我解决方案。很多人说这是window或machine.config的问题。
          • 这不适合在 cmets 中处理。问一个新问题。
          • 简而言之,我遇到了与@Narinder 描述的相同的问题,收到一个错误,说不能/不允许访问此文件。那是因为对于网站,你仍然需要使用相对路径。 SO: use the full path to upload, use or make a relative path and store that in your database or whatever.
          【解决方案6】:

          如果要将上传的文件保存到fp的value中,直接传入即可,不要放在引号中:

          tt.PostedFile.SaveAs(fp);
          

          【讨论】:

            猜你喜欢
            • 2023-03-24
            • 2010-11-23
            • 1970-01-01
            • 2011-12-13
            • 1970-01-01
            • 2018-07-11
            • 1970-01-01
            • 2014-09-26
            • 2015-11-14
            相关资源
            最近更新 更多