C#中的FileNotFoundException

时间:2018-08-11 04:40:15

标签: c# entity-framework code-first filenotfoundexception

我正在尝试将图像转换为字节数组以将其上传到SQL数据库,该项目成功构建,但是在使用实体框架中的代码优先方法进行数据库更新时,我发现文件未找到异常,但是文件位于app_data中。

public override void Up()
    {

        Sql("SET IDENTITY_INSERT Movies ON");
        Image image = Image.FromFile(@"c:\users\erprakash\source\DeltaXIMDB\DeltaXIMDB\App_Data\om.png");
        byte[] byteimage = imageToByteArray(image); 
        Sql($"INSERT INTO Movies(Id,Name,YearOfRelease,Plot,Image) VALUES(1,'Mission Impossible Fallout','2018',{byteimage}");
        Sql("SET IDENTITY_INSERT Movies OFF");
    }

如有任何建议和解决方案,请分享。 谢谢

1 个答案:

答案 0 :(得分:0)

对于MVC3及以下版本:

string file = HttpContext.Current.Server.MapPath("~/App_Data/om.png");

对于MVC4>:

string file = Server.MapPath("~/App_Data/om.png");

您可以从代码中使用它:

Image image = Image.FromFile(file);