[英]Difference in Base64 encoding
I am trying to encode, Image to Base 64 string in AngularJS2 我正在尝试将图像编码为AngularJS2中的Base 64字符串
handleFileSelect(evt) {
var files = evt.target.files;
var file = files[0];
if (files && file) {
var reader = new FileReader();
reader.onload = this._handleReaderLoaded.bind(this);
reader.readAsBinaryString(file);
}
}
_handleReaderLoaded(readerEvt) {
var binaryString = readerEvt.target.result;
this.model.UserProfileImageBase64 = btoa(binaryString);
console.log(this.model.UserProfileImage);
}
I am receiving a different string,in compare to encoding it from Java 与从Java对其进行编码相比,我收到了不同的字符串
Base64.encodeToString(getBytesFromBitmap(bitmap),Base64.NO_WRAP);
Any Idea how can we match both Base64 encoding? 任何想法我们如何才能匹配两种Base64编码? I had tried same with base64 encoding in angular as well 我也尝试过用角度编码的base64编码
this.model.UserProfileImageBase64 = Base64.encode(binaryString);
But no dfiference in result. 但结果无异。
BtoA and Base64 producing same result and, if I am verifying it online I am getting image as well but I need it in the same format which is generated by Java BtoA和Base64产生相同的结果,如果我在线验证它,我也会得到图像,但是我需要使用Java生成的相同格式的图像
I am getting the same result with java and javascript. 我用java和javascript得到相同的结果。
With java I did: 使用Java我做到了:
byte[] imageBytes = IOUtils.toByteArray(new URL("https://www.w3schools.com/css/paris.jpg"));
String base64 = Base64.getEncoder().encodeToString(imageBytes);
System.out.println(base64);
With javascript I did: 使用javascript,我做到了:
const fileReader: FileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onloadend = () => {
retVal.next(fileReader.result);
};
NOTE : I would suggest not to use readBynaryAsString as it is deprecated and might not work. 注意 :我建议不要使用readBynaryAsString,因为它已被弃用并且可能无法正常工作。 FileReader 文件阅读器
NOTE : That in javascript I wait till the file is uploaded to get the data and I passed to an observable next(...) 注意 :在javascript中,我一直等到文件上传后才能获取数据,然后传递给可观察的next(...)
changeListener($event) : void {
this.readThis($event.target);
}
readThis(inputValue: any): void {
var file:File = inputValue.files[0];
var myReader:FileReader = new FileReader();
myReader.onloadend = (e) => {
this.image = myReader.result;
}
myReader.readAsDataURL(file);
}
component.html component.html
<input type="file" accept="image/*" (change)="changeListener($event)">
Follow this link Angular 2 encode image to base64 点击此链接Angular 2将图像编码为base64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.