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

185
Visualizações
file gets crashed after resize in front end

For SEO optimizaion I'm attemting to low off the size of the files that the user attempts to send (I know I could have some size limitation or something not doing so because of the UX). and I'm doing it in the front-end cause I want to use pre-signed URL method (AWS S3)

process(event: any, imageInputElement: any, maxWidth: number): any {
  return new Promise<any>((resolve, reject) => {
    try {
      const file = event.target.files[0]
      console.log('🚀 ~ file: index.vue ~ line 143 ~ process ~ file', file)
      const fileSize = file.size
      if (fileSize < 100000) return
      if (!file) return
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = function (event: any) {
        const src = event.target.result
        const canvas = document.createElement('canvas') as any
        const imgElement = document.createElement('img') as any
        imgElement.src = src
        imageInputElement.src = event.target?.result
        console.log(maxWidth)
        imageInputElement.onload = function (e: any) {
          const scaleSize = maxWidth / e.target.width
          canvas.width = maxWidth
          canvas.height = e.target.height * scaleSize
          const ctx = canvas.getContext('2d')
          ctx.drawImage(e.target, 0, 0, canvas.width, canvas.height)
          const compressPer = (data: number) => {
            const result = 10000000 / data
            if (Math.trunc(result) >= 100) {
              return 100
            } else if (Math.trunc(result) < 1) {
              return 1
            } else {
              return Math.trunc(result)
            }
          }
          const srcEncoded = ctx.canvas.toDataURL(
            e.target,
            'image/jpeg',
            compressPer(fileSize)
          )
          const result = new File([srcEncoded], `${file.name}`, {
            type: 'image/jpeg',
          })
          console.log(
            '🚀 ~ file: index.vue ~ line 186 ~ process ~ result',
            result
          )

          resolve(result)
        }
      }
    } catch (error: any) {
      reject(error)
    }
  })
},

This function gets called every time the user changes a file input.

event: is the default change event that includes the file itself. imageInputElement: is the element that I want to render the new file in it. and maxWidth is the width that I pass to the function to specify the max width

The actual problem: the file will become visible in the browser and gets uploaded to the s3 bucket but the file is crashed when I want to download it again.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

instead of

const file = event.target.files[0]

I should have used

var blobBin = atob(srcEncoded.split(',')[1]);
          var array = [];
          for(let i = 0; i < blobBin.length; i++) {
              array.push(blobBin.charCodeAt(i));
          }
          const result:any = new Blob([new Uint8Array(array)], {type: 'image/png'});

got my answer from here

about 4 years ago · Santiago Gelvez 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