使用Dart下载PNG文件(二进制文件)并显示它不工作 [英] Using Dart to Download a PNG File (Binary File) and displaying it not working

查看:512
本文介绍了使用Dart下载PNG文件(二进制文件)并显示它不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个休息的API,我要求检索一个PNG图片显示在我的页面上。

I have a rest API which I am calling to retrieve a PNG image to display on my page.

我的代码:

void getProfilePicture(var pic_id) {

  request = new HttpRequest();
  request.responseType = "blob";
  request.onReadyStateChange.listen(onPicture); 

  // Get Basic Auth credentials
  var authorization = 'Basic '+storage.loginData['password'];

  // Build JSON
  Map reqData = new Map();
  reqData['id'] = pic_id.toString();
  reqData['type'] = 'WEB_SMALL';

  // SEND the request to the server.
  var url = sifted.serverAPI+'/api/v1/pictures/getpicture';
  request.open('POST', url);
  request.withCredentials = false;
  request.setRequestHeader('Authorization',authorization);
  request.setRequestHeader('Content-Type','application/json');
  request.send(json.stringify(reqData));
}

void onPicture(_) {
  if (request.readyState == HttpRequest.DONE &&
      request.status == 200) {
    Blob blob = new Blob(request.response);

    FileReader reader = new FileReader();
    reader.onLoad.listen((fe) { 
      ImageElement imgInput = query('#profilepic');
      imgInput.src = reader.result;
    });
    reader.readAsDataUrl(blob); 
  }  
}

它不工作,我得到这些错误Dart编辑器:

It does not work and I get these errors in the Dart editor:

Exception: type 'Blob' is not a subtype of type 'List' of 'blobParts'.
Exception: type 'Blob' is not a subtype of type 'List' of 'blobParts'.

有什么建议我做错了什么?

Any suggestions on what I am doing wrong?

谢谢!

推荐答案

问题是这行: / p>

The problem is this line:

Blob blob = new Blob(request.response);

Blob构造函数需要 List 另一个 Blob (其中 request.response 在您的用例): factory Blob blobParts,[String type,String endings])

The Blob constructor expects a List instead of another Blob (which request.response is in your use case): factory Blob(List blobParts, [String type, String endings])

只需删除该行,并直接调用 reader.readAsDataUrl request.response),它应该工作。

Just delete the line, and directly call reader.readAsDataUrl(request.response), and it should work.

这篇关于使用Dart下载PNG文件(二进制文件)并显示它不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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