2011-04-28 204 views
7

上传文件我有一个网站管理部分,我很忙,有4个FileUpload控件用于特定目的。我需要知道的是,当我在FileUpload控件的SaveAs()方法中使用Server.MapPath()方法时,在我上传网站后它仍然可以在Web服务器上使用吗?据我所知,SaveAs()需要一个绝对路径,这就是为什么我地图Server.MapPath()使用Server.MapPath()和FileUpload.SaveAs()

if (fuLogo.HasFile) //My FileUpload Control : Checking if a file has been allocated to the control 
     { 
      int counter = 0; //This counter Is used to ensure that no files are overwritten. 
      string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' }); 
      logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]); // This is the part Im wondering about. Will this still function the way it should on the webserver after upload? 
      if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG") 
      { 
       while (System.IO.File.Exists(logo)) 
       { 
        counter++; //Here the counter is put into action 
        logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]); 
       } 
      } 
      else 
      { 
       cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again."; 
       cvValidation.IsValid = false; 
      } 
      if (fuLogo.PostedFile.ContentLength > 409600) //File must be 400kb or smaller 
      { 
       cvValidation.ErrorMessage = "Please use a picture with a size less than 400 kb"; 
       cvValidation.IsValid = false; 

      } 
      else 
      { 

       if (fuLogo.HasFile && cvValidation.IsValid) 
       { 
        fuLogo.SaveAs(logo); //Save the logo if file exists and Validation didn't fail. The path for the logo was created by the Server.MapPath() method. 
       } 

      } 
     } 
     else 
     { 
      logo = "N/A"; 
     } 
+0

哦,标志是在全球范围内 – Eon 2011-04-28 09:55:54

回答

4
  • 如果您打算将文件保存在 目录Web服务器上,然后 Server.MapPath()将是合适的 解决方案。

    string dirPath = System.Web.HttpContext.Current.Server.MapPath("~") + "/Images/Logos/"+ fileBreak[0] + counter.ToString() + "." + fileBreak[1];

    Here

  • ,如果你打算保存的文件出来 Web服务器,然后

    使用的完整路径,如

    “C:\上传” 并确保网络进程拥有 权限写入该文件夹,我建议你在这种情况下将路径本身存储在web.config文件中。

+1

非常方便您将信息交给了您的第二点。我会研究如何快速做到这一点,然后再在客户支持处弹出另一个静脉 – Eon 2011-04-28 10:08:40

+0

完整路径存储在数据库中,并且我打算将图像上载到图像/徽标目录中。该路径将从数据库中读出,并分解为访问这个新上传图像所需的根路径。 – Eon 2011-04-28 10:10:48

0

的路径是它仍然应该工作为Server.MapPath使用的相对值,并返回完整的物理路径。

+0

结果我有一个名为Test.Png的图像 Ç声明一个字符串变量:/测试网站/Images/Logos/Test0.Png(作为一个新保存的文件),这是我寻找的结果。只是想知道我是否仍然能够访问Web服务器上的文件(当上传到Web服务器时)。它显然会结束在一个相对的图像/标志/目录。只是,当我尝试访问test0.png时,它是否会被我的网站合法上传和使用? – Eon 2011-04-28 10:06:30

3

是的,这可以在保存文件,当您尝试检索该文件后使用......

Server.MapPath("~/Images/Logos/" + uploadedFileName); 
+0

server.MapPath有一个../Images/Logos的原因是它必须返回一个目录,然后进入Images/Logos。我很高兴它可以在网络服务器上运行,因为我在某处读到Server.MapPath()并不总是按预期工作,一旦你的网页上传了。不过谢谢。 – Eon 2011-04-28 10:02:32

+1

尝试始终使用〜符号,该符号将从根目录开始。 – 2011-04-28 10:10:42

+0

所以我应该将其更改为〜/../图片/标志? :? – Eon 2011-04-28 10:14:51

0

这是一行代码:

FileUpload1.PostedFile.SaveAs(Server.MapPath(" ")+"\\YourImageDirectoryOnServer\\"+FileUpload1.FileName);