我想使用JavaScript将数据写入现有文件。我不想在控制台上打印它。我想实际将数据写入abc.txt
。我读了很多回答问题,但在控制台上到处都在打印。在某些地方,他们给出了代码,但它不起作用。所以,请任何人可以帮助我如何实际写入数据到文件。
我引用了代码,但它不工作:它给出了错误:
未捕获TypeError:非法构造函数
在chrome和
SecurityError:操作不安全。
在Mozilla上
var f = "sometextfile.txt";
writeTextFile(f, "Spoon")
writeTextFile(f, "Cheese monkey")
writeTextFile(f, "Onion")
function writeTextFile(afilename, output)
{
var txtFile =new File(afilename);
txtFile.writeln(output);
txtFile.close();
}
那么,我们是否可以只使用Javascript将数据写入文件呢?
发布于 2014-01-09 14:01:37
对此有一些建议-
发布于 2014-01-09 17:23:30
您可以使用Blob
和URL.createObjectURL
在浏览器中创建文件。所有最新的浏览器都是support this。
您不能直接保存您创建的文件,因为这会导致大量的安全问题,但您可以将其作为下载链接提供给用户。在支持download属性的浏览器中,您可以通过链接的download
attribute建议一个文件名。与任何其他下载一样,下载文件的用户将对文件名拥有最终决定权。
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
// returns a URL you can use as a href
return textFile;
};
下面是一个使用这种技术从textarea
中保存任意文本的example。
如果你想立即启动下载,而不是要求用户点击链接,你可以使用鼠标事件来模拟鼠标点击链接,就像Lifecube的answer所做的那样。我已经创建了一个使用这种技术的updated example。
var create = document.getElementById('create'),
textbox = document.getElementById('textbox');
create.addEventListener('click', function () {
var link = document.createElement('a');
link.setAttribute('download', 'info.txt');
link.href = makeTextFile(textbox.value);
document.body.appendChild(link);
// wait for the link to be added to the document
window.requestAnimationFrame(function () {
var event = new MouseEvent('click');
link.dispatchEvent(event);
document.body.removeChild(link);
});
}, false);
发布于 2014-01-09 14:11:02
如果你正在谈论浏览器javascript,出于安全原因,你不能直接将数据写入本地文件。HTML5新的API只能让你读取文件。
但如果你想写入数据,并使用户能够下载为文件到本地。下面的代码可以工作:
function download(strData, strFileName, strMimeType) {
var D = document,
A = arguments,
a = D.createElement("a"),
d = A[0],
n = A[1],
t = A[2] || "text/plain";
//build download link:
a.href = "data:" + strMimeType + "charset=utf-8," + escape(strData);
if (window.MSBlobBuilder) { // IE10
var bb = new MSBlobBuilder();
bb.append(strData);
return navigator.msSaveBlob(bb, strFileName);
} /* end if(window.MSBlobBuilder) */
if ('download' in a) { //FF20, CH19
a.setAttribute("download", n);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function() {
var e = D.createEvent("MouseEvents");
e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
D.body.removeChild(a);
}, 66);
return true;
}; /* end if('download' in a) */
//do iframe dataURL download: (older W3)
var f = D.createElement("iframe");
D.body.appendChild(f);
f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData);
setTimeout(function() {
D.body.removeChild(f);
}, 333);
return true;
}
要使用它:
download('the content of the file', 'filename.txt', 'text/plain');
https://stackoverflow.com/questions/21012580
复制相似问题