JavaScript JSzip将文件添加到每个循环
javascript jszip add file to each loop
您好,我在使用 jszip 的循环时遇到问题 -这是为了替换文本,我打算做 1000 个文件或更多。我想要的是为每个添加数字 i++ 并将其保存到 zip 中的单独文本文件中。但是现在我的代码是这样的,它只显示带有输入文本的最后一个文件 - 请帮助
我希望 zip.add 遍历每个变体 - 但它只写入数字而不是整个输入+数字
.HTML:
<script type="text/javascript" src="jszip.js"></script>
<form>
<label> Enter Block of text </label>
<textarea id="inp" name="original"></textarea>
<label> Here is your result </label>
<textarea id="out" name="result"></textarea>
<label> Enter string to replace </label> <br>
<input type="text" id="ori" name="string"></input>
<label> Enter what to replace it with </label>
<input type="text" id="new" name="replace"></input>
<input type="button" onClick="getin()" value="Click for result">
<input id="button" type="button" onClick="getzip()" value="Create Zip"> </input> </form>
JAVASCRIPT:
var zip = new JSZip();
for (var i = 1; i < 100; i++) {
getin(i)
}
function getin() {
var old = document.getElementById("ori").value;
var reg = new RegExp(old, "gim" );
var e = document.getElementById("new").value;
var str1, str2;
str1 = document.getElementById("inp").value;
if (str1 == "") {null;}
var end = str1.replace (reg, e+i);
document.getElementById("out").value = end;
zip.add("output"+i+".txt", end+"'n")
}
function getzip () {
content=zip.generate();
location.href="data:application/zip;base64,"+content;
}
这将创建一个包含 100 个文件的 zip,这些文件根据您的正则表达式替换具有不同的输出 - 不确定您希望内容发生什么。
var zip;
function getin() {
var old = document.getElementById("ori").value;
var reg = new RegExp(old, "gim");
var e = document.getElementById("new").value;
var str1, str2;
str1 = document.getElementById("inp").value;
if (str1 == "") {
return;
}
zip = new JSZip();
var result = '';
Array(100).fill().forEach(function(v,i) {
var end = str1.replace(reg, e + i);
result += end;
zip.file("output" + i + ".txt", end + "'n")
});
document.getElementById("out").value = result;
}
function getzip() {
if (!zip) { return; }
content = zip.generate();
location.href = "data:application/zip;base64," + content;
}
<script src="http://rawgit.com/Stuk/jszip/master/dist/jszip.min.js"></script>
<form>
<label> Enter Block of text </label>
<textarea id="inp" name="original"></textarea>
<label> Here is your result </label>
<textarea id="out" name="result"></textarea>
<label> Enter string to replace </label>
<br>
<input type="text" id="ori" name="string">
<label> Enter what to replace it with </label>
<input type="text" id="new" name="replace">
<input type="button" onClick="getin()" value="Click for result">
<input id="button" type="button" onClick="getzip()" value="Create Zip">
</form>
相关文章:
- 如何使用for循环添加所有按钮'单击事件
- Foreach循环添加值略有偏离
- foreach对象循环添加到堆栈顶部
- 使用While循环添加到字符串
- 弹出循环:添加数据时停止重复/三次应用/X
- 如何避免重复记录为每个循环添加内部角度
- 将循环添加到 Jquery 滑块
- 单击时将值推送到数组,然后循环添加数组值.Javascript
- 将基于 cheerio 的搜索循环添加到节点简单爬网器中
- Javascript 通过循环添加事件侦听器
- 如何将 while 循环添加到随机发生器
- 使用 array_push() 循环添加元素以创建 JSON 数据
- 对于循环添加被读取为未定义的对象
- Javascript循环添加
- 对使用 for 循环添加到数组时的语法感到困惑
- javascript:循环添加数字
- 通过for循环添加数组值
- 在jQuery中动态推送/创建元素,然后立即循环添加HTML
- 通过数组和FOR循环添加事件监听器
- jQuery循环"添加类"之间的延迟的方法