Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Cómo asegurarse de que el SVG cargado tenga atributos de ancho y alto

Mis usuarios pueden cargar archivos de imagen, que podrían ser SVG.

Necesito asegurarme de que todos los SVG tengan un atributo de ancho y alto para algo que ocurra más adelante.

Si tengo el SVG como un archivo (a través de un tipo de entrada = "archivo"), ¿cómo puedo agregarle un ancho y una altura si falta?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Terminé haciendo lo siguiente, aunque estoy seguro de que podría mejorarse/hacerse de una manera mucho mejor

 let svgReader = new FileReader(); svgReader.onload = function (e) { let svg = $(e.target.result); for(let i =0;i<svg.length;i++) { if (svg[i] instanceof SVGElement) { svg = $(svg[i]); break; } } let width = svg.attr("width"); let height = svg.attr("height"); if (height == null || height == undefined) { svg.attr("height", 300); } if (width == null || width == undefined) { svg.attr("height", 300); } let svgFile = new XMLSerializer().serializeToString(svg[0]); let new file = new File([svgFile], files[index].name, {type: "image/svg+xml"}); }; svgReader.readAsText(files[0]);

Tuve que revisar el SVG para encontrar una instancia del elemento SVG, ya que algunos de los SVG con los que probé tenían etiquetas o comentarios adicionales que, de lo contrario, lo estropearían. Además, el valor 300 para el ancho y la altura se inventó, pero parece estar bien, probablemente podría haber usado las dimensiones del cuadro de vista en su lugar, pero funciona para mí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!