I'm in struggle with downloading a picture and then showing it on a page. Printed base64-encoded string looks wrong; it's not identical with e.g. http://www.freeformatter.com/base64-encoder.html result.
This is my code:
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);
}
}
When you set the responseType you get binary data
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);
//}
});
}
result:
data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With