请问h5 怎么写文件下载到本地?
/**
* 保存本地json文件
* @param Json json文件
* @param FileName 文件名称
*/
saveForWebBrowser(Json: any, FileName: string) {
let JsonString = JSON.stringify(Json);
//@ts-ignore
if (cc.sys.isBrowser) {
let textFileAsBlob = new Blob([JsonString]);
let downloadLink = document.createElement('a');
downloadLink.download = FileName;
downloadLink.innerHTML = 'Download File';
if (window.URL != null) {
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
}
else {
//@ts-ignore
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = () => {
document.body.removeChild(downloadLink);
};
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
}
网友评论