每次保存时覆盖文件是可能的。我有一个HTML文本区域并使用JavaScript将文本保存到文件。当前保存的文件名为:test.txt,test(1).txt,test(2).txt。是否可以让它每次下载时都保存为test.txt。
我用于下载的代码如下:
function saveTextAsFile()
{
var textToWrite = document.getElementById("inputTextToSave").value;
var textFileAsBlob = new Blob([textToWrite], {type:'plan/text'});
var fileNameToSaveAs = "test.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "My Hidden Link";
window.URL = window.URL || window.webkitURL;
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
感谢您的帮助。