如何从textarea保存文件并在下载代码中配置路径?JavaScript
单击下载文件后的结果是在我的计算机上下载文件夹,但单击“保存文件”时,我需要一个特定路径,如“\directory\subdirectory”。如何从textarea保存文件并在下载代码中配置路径?JavaScript,javascript,file,download,path,textarea,Javascript,File,Download,Path,Textarea,单击下载文件后的结果是在我的计算机上下载文件夹,但单击“保存文件”时,我需要一个特定路径,如“\directory\subdirectory”。 如果无法使用javascript,我该怎么做? <input id="inputFileNameToSaveAs" style="width: 400px;"></input> <button onclick="saveTextAsFile()" style="width: 250px;">Save File<
如果无法使用javascript,我该怎么做?
<input id="inputFileNameToSaveAs" style="width: 400px;"></input>
<button onclick="saveTextAsFile()" style="width: 250px;">Save File</button>
function saveTextAsFile()
{
var textToWrite = document.getElementById("inputTextToSave").value;
var textFileAsBlob = new Blob([textToWrite], {type: 'text/plain'});
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL !== null)
{
// 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();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
保存文件
函数saveTextAsFile()
{
var textToWrite=document.getElementById(“InputExtToSave”).value;
var textFileAsBlob=newblob([textToWrite],{type:'text/plain'});
var fileNameToSaveAs=document.getElementById(“inputFileNameToSaveAs”).value;
var downloadLink=document.createElement(“a”);
downloadLink.download=fileNameToSaveAs;
downloadLink.innerHTML=“下载文件”;
if(window.webkitURL!==null)
{
//Chrome允许点击链接
//没有实际将其添加到DOM中。
downloadLink.href=window.webkitURL.createObjectURL(textFileAsBlob);
}否则
{
//Firefox要求将链接添加到DOM中
//在点击之前。
downloadLink.href=window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick=destroyClickedElement;
downloadLink.style.display=“无”;
document.body.appendChild(下载链接);
}
downloadLink.click();
}
函数销毁ClickedElement(事件)
{
document.body.removeChild(event.target);
}
不可能这样做。您可以下载该文件,但不能将其写入特定文件夹。为了安全起见,浏览器运行在沙箱中,无法访问整个计算机
您不希望在输入的随机页面上出现一些javascript来干扰您的计算机文件。我可以使用java吗?