C# 如何制作图像的POST方法?

C# 如何制作图像的POST方法?,c#,api,dapper,C#,Api,Dapper,所以我一直在研究这个API,直到现在我才发现如何制作一个以图像为参数的POST方法。理论上,这就是它应该如何工作: 您将从网页上载图像,然后使用到API的路由,图像信息将发送到数据库,如下所示: 现在,我一直在不同的页面上寻找答案,但没有一个真正有用。事实上,我找到的唯一指南是关于将该方法集成到Web Api中的(请参阅),但这对我来说并不真正有效,因为我无法继承解决方案中的某些方法。例如,使用上面的链接,我遇到了HttpContext.Current的问题,我想这是因为我目前使用的解决方案。

所以我一直在研究这个API,直到现在我才发现如何制作一个以图像为参数的POST方法。理论上,这就是它应该如何工作:

您将从网页上载图像,然后使用到API的路由,图像信息将发送到数据库,如下所示:

现在,我一直在不同的页面上寻找答案,但没有一个真正有用。事实上,我找到的唯一指南是关于将该方法集成到Web Api中的(请参阅),但这对我来说并不真正有效,因为我无法继承解决方案中的某些方法。例如,使用上面的链接,我遇到了HttpContext.Current的问题,我想这是因为我目前使用的解决方案。然而,这是另一个需要问的问题

所以我的方法看起来很像这样:

public class RecipeController : Controller
{
    private readonly Func<SqlConnection> _connFactory;
    public RecipeController(Func<SqlConnection> connFactory)
    {
        _connFactory = connFactory;
    }

    [Route("v1/recipe/{recipeBy}")]
    [HttpGet()]
    public List<Recipe> GetRecipes(string recipeBy)
    {
        using (var con = _connFactory())
        {
            con.Open();
            return con.Query<Recipe>("SELECT * FROM dbo.Recipe WHERE RecipeBy = @recipeBy", new { recipeBy }).ToList();
        }
    }
    ....
公共类RecipeController:控制器
{
私有只读功能工厂;
公共RecipeController(功能控制工厂)
{
_connFactory=connFactory;
}
[路径(“v1/recipe/{recipeBy}”)]
[HttpGet()]
公共列表GetRecipes(字符串recipeBy)
{
使用(var con=\u connFactory())
{
con.Open();
return con.Query(“SELECT*FROM dbo.Recipe,其中RecipeBy=@RecipeBy”,new{RecipeBy}).ToList();
}
}
....
我正在使用Dapper将值传递到数据库


因此,我的问题是:我如何编写一个POST方法,将上传的图像作为参数,然后将其传递到数据库?我确实意识到这个问题相当模糊,因为我甚至没有提供可复制的代码。问题是,直到现在,我甚至没有找到一个正确的方法开始工作,所以我无法真正提供任何您可以帮助我的有用代码。任何提示、提示、建议、教程……欢迎使用!

您可以通过使用Html5 FileReader类在客户端将图像读取为字符串,然后将其发布到api端点来完成此操作:

function UploadImage()
{
    var file = input[0].files[0];

    var reader = new FileReader();

    reader.onloadend = function () {
        var imageDataString = reader.result;
        // post the fileString value to your api here
    }

    if (file) {
        reader.readAsDataURL(file);
    }
}
然后在控制器上,将Base64字符串转换为字节[],并将其存储在数据库中:

if (imageDataString != null && imageDataString != String.Empty)
{
    string imageDataParsed = imageDataString.Substring(imageDataString.IndexOf(',') + 1);
    byte[] imageBytes = Convert.FromBase64String(imageDataParsed);
}

您可以通过使用Html5 FileReader类将图像读取到客户端的字符串中,然后将其发布到api端点来实现这一点:

function UploadImage()
{
    var file = input[0].files[0];

    var reader = new FileReader();

    reader.onloadend = function () {
        var imageDataString = reader.result;
        // post the fileString value to your api here
    }

    if (file) {
        reader.readAsDataURL(file);
    }
}
然后在控制器上,将Base64字符串转换为字节[],并将其存储在数据库中:

if (imageDataString != null && imageDataString != String.Empty)
{
    string imageDataParsed = imageDataString.Substring(imageDataString.IndexOf(',') + 1);
    byte[] imageBytes = Convert.FromBase64String(imageDataParsed);
}

您好!非常感谢您的帮助。但是,我有点困惑。我应该在哪里集成这两个代码?例如,假设我的方法如下所示:
[Route(“v1/recipe/image”)][HttpPost()]public IActionResult InsertImage()
.Hi@Questieme.抱歉,我已经更新了我的答案,因为您需要将其发送到web api控制器。您的公共IActionResult InsertImage()应该有一个字符串参数来接受文件字符串,然后您可以将其转换为字节[]需要时。您好!非常感谢您的帮助。但是,我有点困惑。我到底应该在哪里集成这两个代码?例如,假设我的方法如下:
[Route(“v1/recipe/image”)][HttpPost()]public IActionResult InsertImage()
.Hi@Questieme.抱歉,我已经更新了我的答案,因为您需要将其发送到web api控制器。您的公共IActionResult InsertImage()应该有一个字符串参数来接受文件字符串,然后您可以根据需要将其转换为字节[]。