我想将我的文本文件保存在特定的文件夹/位置来存储它,我应该使用什么代码?我拥有的代码正在保存可以使用 Web 下载的文本文件,而不是将文件保存在特定文件夹/位置,因为我使用的是操作系统:带有 Tomcat Apache 的 Solaris。
文件夹/位置示例:
/tmp/textFileSaveDir/
这是我的代码:
<%--
Document : index
Created on : Mar 7, 2014, 7:33:21 AM
Author : Joseph
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<table>
<tr><td>Text to Save:</td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
</td>
</tr>
<tr>
<td>Filename to Save As:</td>
<td><input id="inputFileNameToSaveAs"></input></td>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
</table>
<script type='text/javascript'>
function saveTextAsFile()
{
<%-- Input Text To Save --%>
var textToWrite = document.getElementById("inputTextToSave").value;
<%-- Creating file To Save As --%>
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
<%-- Input Filename To Save As --%>
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();
}
</script>
</body>
</html>
最佳答案
在我看来你可以使用php来存储文件。可以说,javascript 是客户端,php 是服务器端。
所以理论上你可以使用 php 的一些函数,比如 fwrite、fopen 等等。
这就是我的看法
关于javascript - 在 Web 上使用 JavaScript 将文本文件保存到特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22242175/