cocoscreator downloads pictures and saves them locally

test:function (url, callback) {
    console.log('remote test');
var dirpath =  jsb.fileUtils.getWritablePath() + 'img/';
var filepath = dirpath + 'picture' + '.png';
console.log('remote filepath: ' + filepath);
function loadEnd(){
            cc.loader.load(filepath, function(err, tex){
                if( err ){
                    cc.error(err);
}else{
                    var spriteFrame = new cc.SpriteFrame(tex);
if( spriteFrame ){
                        spriteFrame.retain();
callback(spriteFrame);
}
                }
            });
}

        if( jsb.fileUtils.isFileExist(filepath) ){
            console.log('Remote is find' + filepath);
loadEnd();
return;
}

        var saveFile = function(data){
            if( typeof data !== 'undefined' ){
                if( !jsb.fileUtils.isDirectoryExist(dirpath) ){
                    etc. fileUtils .createDirectory (dirpath) ;
}

                if( jsb.fileUtils.writeDataToFile(  new Uint8Array(data) , filepath) ){
                    console.log('Remote write file succeed.');
loadEnd();
}else{
                    console.log('Remote write file failed.');
}
            }else{
                console.log('Remote download file failed.');
}
        };
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
            console.log("Remote xhr.readyState  " +xhr.readyState);
console.log("Remote xhr.status  " +xhr.status);
if (xhr.readyState === 4 ) {
                if(xhr.status === 200){


                    xhr.responseType = 'arraybuffer';
saveFile(xhr.response);
}else{
                    saveFile(null);
}
            }
        }.bind(this);
xhr.open("GET", url, true);
xhr.send();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326058643&siteId=291194637