升级cocos creator1.7.2以后如下代码无法运行,1.6.1是可以正常运行得到数据的,这里转为Uint8Array后的数据为空.
var saveFile = function (data) {
if (!data || typeof data == ‘undefined’ || data.length == 0) {
callback(mm.data.avatar_default);
return;
}
if (!jsb.fileUtils.isDirectoryExist(dirpath)) {
jsb.fileUtils.createDirectory(dirpath);
}
var data8 = new Uint8Array(data);
if(!data8 || data8.length == 0) {
callback(mm.data.avatar_default);
return;
}
if (jsb.fileUtils.writeDataToFile(data8, filepath)) {
cc.log('Remote write file succeed.');
loadEnd();
}
else {
cc.log('Remote write file failed.');
}
};
//var t = url.substr(0, 2);
var loadpath = avatar_url;
if (loadpath == null || loadpath.length == 0) {
loadpath = mm.config.avatarHost + account_id + ".jpg";
}
var xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 10000; //毫秒单位
xhr.ontimeout = function () {
xhr.abort(); //重置
//重新请求
xhr.open("GET", loadpath, true);
xhr.send();
}.bind(this);
xhr.onerror = function (error) {
callback(mm.data.avatar_default);
console.log("getXMLHttpRequest,xhr.onerror")
}.bind(this);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 304) {
xhr.responseType = 'arraybuffer';
saveFile(xhr.response);
} else {
saveFile(null);
}
}
}.bind(this);
xhr.open("GET", loadpath, true);
xhr.send();
