javascript - 如何在 JavaScript(本地文件)中一次转换多个文件?
问题描述
我创建了一个转换器,它接收一个 SVG 文件并给我一个文本文件作为输出,并进行了一些修改。如何修改它以应用于一批文件?
我尝试使用 multiple="multiple",但是当我选择输入时,它没有给我选择多个文件的选项。
document.getElementById("fileReader").addEventListener('change', function() {
var filePath = document.getElementById("fileReader").value;
var fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("\."));
var fr = new FileReader();
fr.onload = function() {;
var d = new DOMParser().parseFromString(this.result.toString(), "image/svg+xml");
var textOut = "";
//various conversion operations.
function download(filename, text, type = "text/plain") {
// Create an invisible A element
const a = document.createElement("a");
a.style.display = "none";
document.body.appendChild(a);
// Set the HREF to a Blob representation of the data to be downloaded
a.href = window.URL.createObjectURL(
new Blob([text], {
type
})
);
// Use download attribute to set set desired file name
a.setAttribute("download", filename);
// Trigger the download by simulating click
a.click();
// Cleanup
window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}
// Starting file download.
document.getElementById("button").onclick = function() {
download(fileName, textOut);
};
}
fr.readAsText(this.files[0]);
})
<input type="file" id="fileReader" multiple="multiple" />
<input id="button" type="button"/>
哪些更改可以让我一次对多个文件执行此操作?我希望选择的每个文件都被转换并作为单独的文件下载。有没有办法对整个文件夹执行此操作?
解决方案
推荐阅读
- python - 如何在 Windows 64、Python 3.7 上为 Geonode 3.2 安装和配置 memchached
- c# - 如何以角度在服务器上添加文件和对象?
- python - 尝试将多台计算机连接到 MySQL 服务器
- asp.net - 将可以 iframe 构建 asp.net mvc Web 应用程序的域列入白名单的正确且安全的方法是什么?
- java - 如何从 maven 运行导入的 jar/依赖项
- sql - 分区窗口中两行之间的 SQL 时间差
- java - Java获取两种不同的方法
- python - 我们如何将列表转换为数据框?
- spring-cloud-sleuth - How to skip tracing of http OPTIONS requests
- macos - 如何在Objective C中使用Applescript在提示密码时启用Touch ID