【问题标题】:request.files adds the path on localhostrequest.files 在 localhost 上添加路径
【发布时间】:2010-06-26 00:59:44
【问题描述】:

我正在使用 Request.Files 获取用户在我的网页上上传的文件。

我注意到,如果我在 IIS 中使用文件名属性,它会给我一个路径 + 文件名,但是如果我在 cassini 中运行,无论我使用什么目录,它都只会给我文件名。

这是为什么?而且,有没有办法在 IIS 中只使用文件名?

谢谢, 棒。

【问题讨论】:

  • 好的,问题略有不同,但问题是一样的。使服务器脚本可以使用完整的客户端路径是一个潜在的安全风险。较新的浏览器在上传文件时只发送文件名,而不是路径。

标签: c# asp.net asp.net-mvc


【解决方案1】:

要获取文件名,请仅使用:

System.IO.Path.GetFileName(userPostedFile.FileName));

喜欢:

HttpFileCollection uploadedFiles = Request.Files;

   for (int i = 0; i < uploadedFiles.Count; i++)
   {    
      HttpPostedFile userPostedFile = uploadedFiles[i];

      if (userPostedFile.ContentLength > 0 )
      {
        string fileName = System.IO.Path.GetFileName(userPostedFile.FileName));
      }


   }

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多