CocosCreator 中如何下载文件

const fileName = "haha";
const fileType = ".png"
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
    if (xhr.readyState === 4 && xhr.status === 200) {
        if (xhr.response && cc.sys.isNative) {
            const rootPath = jsb.fileUtils.getWritablePath();
            const filtPath = rootPath + fileName + fileType;

            const u8a = new Uint8Array(xhr.response);
            jsb.fileUtils.writeDataToFile(u8a, filtPath);
        }
    }
}
xhr.responseType = "arraybuffer";
xhr.open("GET", url, true);
xhr.send();

tip:有用的话请在右侧点哦!

你可能感兴趣的:(cocos,creator)