-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
72 lines (56 loc) · 2.26 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function processFile() {
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('Por favor, selecione um arquivo CSV.');
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function (event) {
const contents = event.target.result;
console.log('Conteúdo do arquivo CSV:', contents); // Adicione esta linha
Papa.parse(contents, {
header: true, // Indica que a primeira linha contém títulos das colunas
complete: function (results) {
const data = results.data;
if (data.length <= 0) {
alert('A tabela não contém dados.');
return;
}
const renamedImages = [];
const numeroDeImagens = data.length;
console.log('Número de imagens a processar:', numeroDeImagens); // Adicione esta linha
data.forEach((row) => {
const nome = row.NOME;
const url = row.IMAGEM;
renamedImages.push({ nome, url });
});
console.log('Dados das imagens renomeadas:', renamedImages); // Adicione esta linha
const zip = new JSZip();
const imagePromises = []; // Declare a variável imagePromises
renamedImages.forEach((image) => {
const imagePromise = fetch(image.url)
.then((response) => response.blob())
.then((blob) => {
zip.file(`${image.nome}.jpg`, blob);
});
imagePromises.push(imagePromise);
});
// Aguarda o download de todas as imagens
Promise.all(imagePromises).then(() => {
console.log('Todas as imagens foram baixadas com sucesso.'); // Adicione esta linha
// Gera o arquivo ZIP e cria o link de download
zip.generateAsync({ type: 'blob' }).then((content) => {
const url = URL.createObjectURL(content);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = url;
downloadLink.download = 'imagens_renomeadas.zip';
downloadLink.textContent = `Clique aqui para baixar as ${numeroDeImagens} imagens renomeadas`;
downloadLink.style.display = 'block';
});
});
},
});
};
reader.readAsText(file);
}