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

506
Visualizações
How can I duplicate a file (completely) but change it's name?

So I need to generate the smaller previews of the image files that will be uploaded and I have to append "_preview" at the end of each file name.

Currently I'm doing this:

uploadFile.map((file) => {
      if (file.type.includes('image')) {
        console.log('Generating thumbnail for ' + file.name)
        const fileName = file.name.split('.').slice(0, -1).join('.')
        const fileExtension = file.name.split('.').pop()
        const compressedFile = new File(
          [file.slice(0, file.size, file.type)],
          fileName + '_preview.' + fileExtension,
        )
        console.log('Generated file:', compressedFile)
        convert({
          file: compressedFile,
          width: 300,
          height: 300,
          type: fileExtension,
        })
          .then((resp) => {
            uploadFile.push(resp)
          })
          .catch((error) => {
            // Error
            console.error('Error compressing ', file.name, '-', error)
          })
      }
    })

The problem is that "compressedFile" is missing some fields which were present in the original file and so the convert functoin throws the error "File type not supported". As you can see "type" and "webkitRelativePath" are not copied.

enter image description here

Can anybody suggest how I can retain all the information from the original file and just append _preview at the end of file name?

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

0

I realized File API provides an option to pass "options" object as well which can specify the file type. For instance:

const file = new File(["foo"], "foo.txt", {
  type: "text/plain",
});

Source: https://developer.mozilla.org/en-US/docs/Web/API/File/File

about 4 years ago · Santiago Gelvez Relatório

0

for copy code in js or duplicate, you can use this code


    //copyfile.js
    const fs = require('fs');
    
    // destination will be created or overwritten by default.
    fs.copyFile('C:\folderA\myfile.txt', 'C:\folderB\myfile.txt', (err) => {
      if (err) throw err;
      console.log('File was copied to destination');
    });

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