2017-08-28 74 views
-2

我已經檢查了很多關於這個問題的答案,沒有任何東西可以幫助我。我的情景是我有一個表中有一列添加圖像作爲字節數組。我將模型屬性聲明爲數組來存儲圖像字節。然後在遷移配置類中,我將數據播種到包含圖像的表格中。File.readallbytes沒有找到圖像文件

var gembymonth = new List<GemStoneByMonth>{ 
      new GemStoneByMonth 
      { 
       EnglishZodiac="Aries", 
       SanskritZodiac = "Mesa", 
       GemEng="Garnet",    

       GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/GarnetGem.png") 
} 

我在這裏提供的文件路徑是爲我的本地系統,它運作良好。如果我給任何其他類型的路徑它會引發錯誤。如果我添加〜/ content /「文件名」,因爲我的圖像在我的內容文件夾中,當我將它託管到我的服務器時,它會返回明顯的錯誤「找不到部分路徑」。我已經嘗試了很多方法,如

Server.MapPath 但server.path說它不包含定義,當我解析我得到system.sql命名空間,然後mappath shoes錯誤。

我的模型類

public class GemStoneByMonth 
    { 

     public int GemStoneByMonthId { get; set; } 
     public string EnglishZodiac { get; set; } 
     public string MalayalamZodiac { get; set; } 
     public string SanskritZodiac { get; set; } 
     public string GemEng { get; set; } 
     public string GemMal { get; set; } 
     public byte[] GemImage { get; set; } 
    } 
} 

遷移配置種子法

protected override void Seed(Astrology.AstroDatabase.ApplicationDbContext context) 
     { 

      var gembymonth = new List<GemStoneByMonth>{ 
      new GemStoneByMonth 
      { 
       EnglishZodiac="Aries",     
       SanskritZodiac = "Mesa", 
       GemEng="Garnet",         
       GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/GarnetGem.png") 
      // GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/GarnetGem.png") 
      }, 

      new GemStoneByMonth 
      { 
       EnglishZodiac = "Taurus",    
       SanskritZodiac = "Vrsabha", 
       GemEng = "Lapis Lazuli",     
       GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/LapisLazuli.png") 

      }, 

      new GemStoneByMonth 
      { 
       EnglishZodiac = "Gemini",    , 
       SanskritZodiac = "Mithuna", 
       GemEng = "Aquamarine",     
       GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/Aquamarine.png") 
      }, 

      new GemStoneByMonth 
      { 
       EnglishZodiac = "Cancer",     
       SanskritZodiac = "Karka", 
       GemEng = "Yellow Saphire",     
       GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/Yellow Saphire.png") 
      }, 

      new GemStoneByMonth 
      { 
       EnglishZodiac = "Leo",     
       SanskritZodiac = "Siṃha", 
       GemEng = "Red Coral",     
       GemImage = File.ReadAllBytes("E:/NavBar/Astrology/Astrology/Astrology/Content/Images/Redcoral.png") 

      }, 

}; 

      gembymonth.ForEach(p => context.GemStoneByMonths.AddOrUpdate(s => new { s.EnglishZodiac, s.SanskritZodiac, s.GemEng, s.GemImage }, p)); 

     } 
     } 

我已經使用這個文件路徑,因爲沒有其他的工作。我知道它不會在服務器上工作,因爲它是我的本地文件路徑。

我在這上面撓了幾個小時。可以有人說如何正確映射我的文件,因爲我託管我的網站到服務器。有人可以幫我弄這個嗎?

+1

所以文件不會在服務器上該位置存在。 – CodeCaster

+0

發佈您真正使用的代碼,包括server.mappath和all,以及調用堆棧的完整異常。可能您的本地系統上存在的路徑不在服務器上。在這種情況下,server.mappath方法聽起來像是正確的做法,特別是當您使用沒有固定盤符和根目錄的相對路徑時。文件系統路徑應該使用反斜槓而不是正斜槓,儘管現代操作系統會理解這兩者,我不認爲這是這裏的問題。 – dlatikay

+0

但是當我使用server.mappath時會拋出錯誤。它不包含定義, – Abhijith

回答

0

您將需要使用HttpRuntime.AppDomainAppPathServer.MapPath

例如,

string path = HttpRuntime.AppDomainAppPath + "\\Content\\Images\\"; 
// Or string path = Server.MapPath("~/Content/Images/"); 
var gembymonth = new List<GemStoneByMonth> 
{ 
    new GemStoneByMonth 
    { 
     EnglishZodiac = "Cancer", 
     SanskritZodiac = "Karka", 
     GemEng = "Yellow Saphire", 
     GemImage = path + "Yellow Saphire.png" 
    } 
}; 
+0

讓我檢查這一個 – Abhijith