gpt4 book ai didi

dart - 如何在Dart中正确下载图片并转换为base64?

转载 作者:行者123 更新时间:2023-12-02 09:30:36 28 4
gpt4 key购买 nike

我很难下载图片然后将其显示在页面上。打印的 Base64 编码字符串看起来错误;它与例如不同http://www.freeformatter.com/base64-encoder.html结果。

这是我的代码:

HttpRequest.request(_url).then((HttpRequest response) {
String contentType = response.getResponseHeader('Content-Type');

if (_supportedContentType(contentType)) {
List bytes = new Utf8Encoder().convert(response.responseText);
String header = 'data:$contentType;base64,';
String base64 = CryptoUtils.bytesToBase64(bytes);
String image = "${header}${base64}";

me.avatar = image;
print(image);
}
}

最佳答案

当你设置responseType时,你会得到二进制数据

import 'dart:html' show HttpRequest;
import 'dart:convert' show base64;
import 'dart:typed_data' show Uint8List, ByteBuffer;

main() {
HttpRequest
.request("Zacharie_Noterman_-_Monkey_business.jpg",
responseType: "arraybuffer")
.then((HttpRequest response) {
String contentType = response.getResponseHeader('Content-Type');

var list = new Uint8List.view((response.response as ByteBuffer));

String header = 'data:$contentType;base64,';
String base64 = base64.encode(list);
String image = "${header}${base64}";

// me.avatar = image;
print(image);
//}
});
}

结果:

data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD....

关于dart - 如何在Dart中正确下载图片并转换为base64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24935653/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com