在Firefox / Windows下调用此代码时,它可以正常工作,但是,在Firefox / Linux中调用时,它会引发以下错误:
private native String getBlobUrl(String b64)/*-{
function base64ToBlob(base64) {
var binary = atob(base64);
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for ( var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
var blob = new Blob([ view ]);
return blob;
};
var newBlob = base64ToBlob(b64);
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(newBlob);
return source;
}-*/;
错误:
我的代码有什么问题?
我认为错误在此行内(但在Windows环境下仍可以正常工作):
var blob = new Blob([ view ]);
最佳答案
在gwt jsni方法中,您需要使用“$ wnd”。前缀而不是“窗口”。注意,atob()实际上是window.atob()。您需要在jsni代码中为所有window.xxx方法添加此前缀(在“atob(base64)”,“URL”和“webkitUrl”之前):
private native String getBlobUrl(String b64)/*-{
function base64ToBlob(base64) {
var binary = $wnd.atob(base64);
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
var blob = new Blob([ view ]);
return blob;
};
var newBlob = base64ToBlob(b64);
var URLObj = $wnd.URL || $wnd.webkitURL;
var source = URLObj.createObjectURL(newBlob);
return source;
}-*/;
关于jsni的好文章:
http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html