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