Response.Body.WriteAsync

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

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.

Here is the very simple HTML:

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

And my controller:

[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();
    }
}

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.

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).

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

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

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!");
});

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

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

解决方案
 

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

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);

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

 

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



这是非常简单的HTML:



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


我的控制器:



  [Route("api / [controller]")] 
公共类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;

等待Response.Body.WriteAsync(Encoding.UTF8.GetBytes(_ base64Image),0,_ base64Image。长度);
//等待Response.Body.FlushAsync();
}
}


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



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



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



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



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



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


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



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


解决方案
 

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



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



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

 

posted @ 2021-03-11 13:10  dreamw  阅读(1501)  评论(0编辑  收藏  举报