我是javascript尝试从我的电脑上的.htm文档创建.txt文件的新手。
以下代码适用于IE,并在我的桌面上创建一个文件(例如“A1.txt”)。但是,我无法使用Chrome创建文件。我正在运行Windows Vista。
让我知道使用Chrome运行此代码需要进行哪些修改。
提前致谢
String eventCoin = Util.getInstance().getEnhanceModel().getEventCoin();
if (eventCoin == null || eventCoin.equals("")){
Toast.makeText(context, "Can't get link. Try again!", Toast.LENGTH_SHORT).show();
return;
}
AccountModel accountModel = Util.getInstance().getAccount();
String strAuthenAccount = String.format("%d|%s|%s", accountModel.getAccountId(),
accountModel.getAccount(), accountModel.getAccessToken());
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhh");
Date date = new Date();
String signKey = strAuthenAccount + "social)3@0!5" + dateFormat.format(date);
signKey = HashUtil.md5(signKey);
String link = String.format(
eventCoin+"?autheninfo=%s&sign=%s",
strAuthenAccount, signKey);
ShowWapView showWapView = new ShowWapView(context);
showWapView.show(link);
请帮忙。
答案 0 :(得分:1)
使用HTML5 File API
function dp() {
if ('Blob' in window) {
var fileName = "A" + document.F1.T1.value;
var textToWrite = "Test";
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/plain'
});
var downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.innerHTML = "Download File";
if ('webkitURL' in window) {
// Chrome allows the link to be clicked without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
} else {
// Firefox requires the link to be added to the DOM before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
} else {
alert('Your browser does not support the HTML5 Blob.');
}
}
查看更详细的示例here。