ASP.NET Core API Controller:Response.Body.WriteAsync base64字符串无法正常工作 [英] ASP.NET Core API Controller: Response.Body.WriteAsync base64 string not working

查看:2110
本文介绍了ASP.NET Core API Controller:Response.Body.WriteAsync base64字符串无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从API控制器返回表示jpeg图像的base64字符串,并将其设置为< img> 的src,但我的所有尝试都失败了。

I'm trying to return a base64 string representing a jpeg image from an API Controller and set it as the src of an <img> but all my attempts failed.

这是非常简单的HTML:

Here is the very simple HTML:

<img src="/api/TestBase64Image" alt="image test" />

我的控制器:

[Route("api/[controller]")]
public class TestBase64ImageController : Controller
{
    private const string _base64Image = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/7Sfg.....";
    private const string _base64Image2 = "/9j/4AAQSkZJRgABAQEBLAEsAAD/7Sfg.....

    [HttpGet]
    public async Task Get()
    {
        Response.ContentType = "image/jpeg";
        //Response.ContentType = "text/plain";
        //Response.ContentType = new MediaTypeHeaderValue("image/jpeg").ToString();
        //Response.ContentType = new MediaTypeHeaderValue("text/plain").ToString();

        //Response.Headers.Add("Content-Length", _base64Image.Length.ToString());
        //HttpContext.Response.ContentLength = _base64Image.Length;

        await Response.Body.WriteAsync(Encoding.UTF8.GetBytes(_base64Image), 0, _base64Image.Length);
        //await Response.Body.FlushAsync();
    }
}

我尝试过几件事,比如删除 FlushAsync(),改变定义 ContentType ,包括 data:image / jpeg; base64 ,在字符串中或者没有,但没有任何工作。

I've tried several things, like removing FlushAsync(), changing the way to define the ContentType, include data:image/jpeg;base64, in the string or not but nothing is working.

我见过这里这里写在Response体流中是可行的,所以我认为我'好的方式(我之前尝试过返回一个简单的字符串但不能正常工作)。

I've seen here and here that writing in the Response body stream is doable, so I presume I'm on the good way (I've tried to return a simple string before but not working as well).

是的,我的base64字符串是正确的,因为我也尝试过将它直接包含在HTML中,图像显示正确。

Yes my base64 string is correct because I've also tried to include it directly into the HTML and the image shows up correctly.

我确切地说我不想使用任何JavaScript或Razor视图来实现这一点,只有纯HTML和我的控制器。

I precise I don't want to use any JavaScript or Razor view to achieve that, only pure HTML and my controller.

另外请注意我在API控制器中。我不能像在空的ASP.NET的 Startup 类中的 Configure 方法中看到的那样做核心项目如:

Also please pay attention that I'm inside an API Controller. I can't do the same as we see in the Configure method in the Startup class of an empty ASP.NET Core project like:

app.Run(async (context) =>
{
    await context.Response.WriteAsync("Hello World!");
});

即使两个回复的类型都是 HttpResponse ,我的控制器中的那个没有任何 Response.WriteAsync 但是 Response.Body.WriteAsync

Even though both Responses' types are HttpResponse, the one in my controller doesn't have any Response.WriteAsync but Response.Body.WriteAsync

感谢您的帮助,我正在搜索几个小时,但对于ASP.NET Core几乎没有相关的资源

Thanks for your help, it's been hours I'm searching but there is almost no resources about this yet for ASP.NET Core

推荐答案

你仍然将数据作为base64文本返回,基本上 - 你得到的是UTF-8编码形式,但就是这样。

You're still returning the data as base64 text, basically - you're getting the UTF-8 encoded form of it, but that's all.

如果你真的只有图像的base64版本,你只需解码:

If you really only have the base64 version of the image, you just need to decode that:

byte[] image = Convert.FromBase64String(_base64Image2);
await Response.Body.WriteAsync(image, 0, image.Length);

(请注意,它只需要您转换的base64 - 而不是数据URI。)

(Note that it needs to be just the base64 you convert - not the data URI.)

这篇关于ASP.NET Core API Controller:Response.Body.WriteAsync base64字符串无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆