![](/img/trans.png)
[英]Any way to hide CSS and JavaScript file from the client-side user?
[英]Creation of a text file from Javascript (or any other way) on the client side?
我想从Javascript创建一个文本文件。 用户将提交表单,表单将有多种选择。 用户将选择适当的答案并单击“提交”按钮。 现在这些答案将放在该文本文件中。 为了设计这个,我创建了HTML文件。 现在我的Javascript有问题。 请告诉我,有没有其他方式而不是JavaScript?
使用您想要的任何文本的字符串
var str = 'Hello world!';
1.创建MIME类型为text / plain的Blob
var b = new Blob([str], {type: 'text/plain'});
2.从Blob生成URL
var fileURL = URL.createObjectURL(b);
3.以您喜欢的方式将您的用户指向它,例如
window.open(fileURL, '_blank');
或者,如果你想下载这个
var a = document.createElement('a'),
e = document.createEvent("MouseEvents"); // simulated click
e.initMouseEvent("click", true, false, self,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.setAttribute('href', fileURL);
a.setAttribute('target', '_blank'); // fallback behaviour
a.setAttribute('download', 'myTextFile.txt'); // file name
a.dispatchEvent(e); // download
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.