Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
javascript soltar y arrastrar, que mostrar información de la imagen

Problema: ¿Cómo puedo obtener el nombre, el tamaño, la extensión y la visualización de la imagen en el div 'imgDetail'

actualización: el nombre del archivo se imprime como Nombre: ${file.name} en lugar del nombre del archivo

botón de arrastrar y soltar archivo html

 <div id="dropZone" style="width: 300px; height: 100px; background-color: red"></div>

mostrar información de la imagen

 <div id="imgDetail"></div>

secuencia de comandos de arrastrar y soltar

 <script> var dropZone = document.getElementById('dropZone'); var detailsDiv = document.querySelector('#imgDetail'); // Optional. Show the copy icon when dragging over. Seems to only work for chrome. dropZone.addEventListener('dragover', function (e) { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; }); // Get file data on drop dropZone.addEventListener('drop', function (e) { document.getElementById('dropZone').classList.remove("hoverActive"); e.stopPropagation(); e.preventDefault(); var files = e.dataTransfer.files; // get all files Object.values(files).forEach((file) => { //loop though all files if (file.type.match(/image.*/)) { var reader = new FileReader(); reader.onload = function (e2) { var img = document.createElement('img'); img.src = e2.target.result; img.width = 100; img.height = 100; detailsDiv.innerHTML += '<p>Size: ${bytesToSize(file.size)}</p>'; //display image var div = document.getElementById('imageHold') div.appendChild(img); }; reader.readAsDataURL(file); }// end of if });// end of for }); function bytesToSize(bytes) { var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes == 0) return '0 Byte'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; } </script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Para mostrar el nombre o el tamaño de la imagen en un div, simplemente use la propiedad innerHTML y la función bytesToSize para formatear el tamaño del archivo.

 var dropZone = document.getElementById('dropZone'); var details = document.querySelector('#imgDetail'); dropZone.addEventListener('dragover', function(e) { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; }); dropZone.addEventListener('drop', (e) => { e.stopPropagation(); e.preventDefault(); details.innerHTML = ''; var files = e.dataTransfer.files; Object.values(files).forEach((file) => { var reader = new FileReader(); reader.onloadend = () => { details.innerHTML += ` <p>Name: ${file.name}<p> <p>Size: ${bytesToSize(file.size)}</p> <img src="${reader.result}" alt="${file.name}">`; }; reader.readAsDataURL(file); }); }); function bytesToSize(bytes) { var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes == 0) return '0 Byte'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; }
 img { width: 200px; height: 100px; object-fit: cover; }
 <div id="dropZone" style="width: 300px; height: 100px; background-color: red"></div> <div id="imgDetail"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda