Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数据库中获取字节[]图像作为文件_C#_Asp.net_Sql Server_Http_Dapper - Fatal编程技术网

C# 从数据库中获取字节[]图像作为文件

C# 从数据库中获取字节[]图像作为文件,c#,asp.net,sql-server,http,dapper,C#,Asp.net,Sql Server,Http,Dapper,我正在制作一个API,可以上传到数据库并从中检索图像。第一部分完成-图像存储在ifformfile变量中,并作为bytes[]传递到数据库 图像型号: public class Image { public int recipeId { get; set; } public string format { get; set; } public string description { get; set; } public IFormFile image { get

我正在制作一个API,可以上传到数据库并从中检索图像。第一部分完成-图像存储在
ifformfile
变量中,并作为
bytes[]
传递到数据库

图像型号:

public class Image
{
    public int recipeId { get; set; }
    public string format { get; set; }
    public string description { get; set; }
    public IFormFile image { get; set; }
}
这就是我目前试图编写GET方法的方式。我的想法是从数据库中获取字节,并将它们转换成一个文件,之后可以在网页上看到

 [Route("v1/recipe/image/{recipeId}")]
 [HttpGet()]
 public IActionResult GetImage(int recipeId)
 {
     byte[] data;
     try
     {
         using (var con = _connFactory())
         {
             data = con.Query("SELECT Image FROM RecipeImage WHERE RecipeId = @recipeId", new { recipeId }).FirstOrDefault();
         }

         return File(new MemoryStream(data), "image/jpeg", "SomeName.jpg");
     }
     catch (Exception exc)
     {
         return BadRequest();
     }
 }
exc
看起来像:

{“无法将类型“object”隐式转换为“byte[]”。存在显式转换(是否缺少强制转换?)}

我不知道这个代码是否应该是正确的,但是
data
总是空的。我还在SSMS中尝试了SQL语句(
SELECT Image FROM RecipeImage,其中RecipeId='140'
),它产生了正确的输出:


有没有其他方法可以让我实现我想要做的事情?

你不是在告诉我应该是什么类型的

这应该做到:

using (var con = _connFactory())
{
    data = con.Query<byte[]>("SELECT Image FROM RecipeImage WHERE RecipeId = @recipeId", new { recipeId }).FirstOrDefault();
}
使用(var con=\u connFactory())
{
data=con.Query(“从RecipeImage中选择图像,其中RecipeId=@RecipeId”,new{RecipeId}”).FirstOrDefault();
}
就我个人而言,我更喜欢:

使用(var con=\u connFactory())
{
data=con.QuerySingle(“从RecipeImage中选择图像,其中RecipeId=@RecipeId”,新的{RecipeId});
}

如果找到多个或少于一个图像,将引发异常。

是<代码>无法将“DapperRow”类型的对象强制转换为“System”类型。字节[]是我得到的。我已经检查了该对象,但无法确定如何使其在我的部分工作。。。