2011-04-28 199 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);