我已经检查了很多这方面的答案,没有什么可以帮助我。我的场景是我有一个表,其中有一列用于将图像添加为字节数组。我将model属性声明为存储图像字节的数组。然后在迁移配置类中,我将数据播种到包含图像的表格。
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 /&#34; filename&#34;因为我的图像在我的内容文件夹中,当我将它托管到我的服务器时,它返回明显的错误&#34;无法找到路径的一部分&#34;。我尝试过很多方法,比如
使用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));
}
}
我使用过这个文件路径,因为其余的都没有。我知道它显然不会在服务器中作为我的本地文件路径。
我在这个问题上摸不着头脑。当我将我的网站托管到服务器时,有人可以说如何正确映射我的文件。有人可以帮我这个吗?
答案 0 :(得分:0)
您需要使用HttpRuntime.AppDomainAppPath
或Server.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"
}
};