我有一个代理来自 Active Directory LDAP 服务器的请求的 NodeJS / Express RESTful API。我这样做是因为 LDAP 查询往往很慢。我使用 RESTful API 间歇性地缓存和刷新数据。我最近尝试添加缩略图。在研究中,我使用 ldapjs 的库似乎正在将本机 ldap 字节数组转换为字符串。
这看起来像的例子:
\ufffd\ufffd\ufffd\ufffd\u0000\u0010JFIF\u0000\u0001\u0000\u0001\u0000x\u0000x\u0000\u0000\ufffd\ufffd\u0000\u001fLEAD Technologies Inc. V1.01\u0000000ufffd\ufffd\u \ufffd\u0000\u0005\u0005\u0005\b\
由于这个事实,图像无法在 Angular 客户端应用程序上正确呈现。所以根据我的研究,这里有一些纠正这个问题的尝试:
使用不同的方法将字符串转换为字节数组(参见代码示例)
修改 ldapjs 库,将数据渲染为 RESTFUL 中的字节数组,如下所示,然后将字节流绑定到角度页面:
html绑定:
<div>
<img *ngIf="userImage" [src]="userImage" alt="{{dataSource.sAMAccountName}}">
</div>
控制器:
public get userImage() {
let value = null;
if(this.dataSource.thumbnailPhoto) {
const byteArray = this.string2Bin(this.dataSource.thumbnailPhoto);
const image = `data:image/jpeg;base64,${Buffer.from(byteArray).toString('base64')}`;
value = this.domSanitizer.bypassSecurityTrustUrl(image);
}
return value;
}
private string2Bin(str) {
var result = [];
for (var i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i));
}
return result;
}
和
控制器的替代版本:
public get userImage() {
let value = null;
if(this.dataSource.thumbnailPhoto) {
const byteArray = new TextEncoder().encode(this.dataSource.thumbnailPhoto);
const image = `data:image/jpeg;base64,${Buffer.from(byteArray).toString('base64')}`;
value = this.domSanitizer.bypassSecurityTrustUrl(image);
}
return value;
}
另一个替代版本的控制器:
public get userImage() {
let value = null;
if(this.dataSource.thumbnailPhoto) {
const blob = new Blob( [Buffer.from(this.dataSource.thumbnailPhoto).toString('base64')], { type: 'image/jpeg' } );
const value = window.URL.createObjectURL(blob);
return value;
}
我希望在角度页面上有一个渲染图像,但我得到的只是未渲染的占位符。
这是我正在使用的库的版本
- 角 - 8.0.3
- NodeJS - 10.15.0
- ldapjs - 1.0.2
我确定我错过了一些东西,我只是不确定它是什么。任何援助将不胜感激。