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

208
Visualizações
Removing Specific Files With JavaScript and HTML

I have a script which is supposed to delete the files which have a file type of PDF in a specific path upon a button being pressed, this is my code.

<a class="btn btn-primary" href="{{ pdflocation }}" id='downloadbutton' role="button">📁Download</a>
    <script>

    function deletepdf() {
        try {
          const path = 'C:/Users/Arshia's PC/PycharmProjects/Flask/static/'
          // Read the directory given in `path`
          fs.readdirSync(path).forEach((file) => {
              // Check if the file is with a PDF extension, remove it
              if (file.split('.').pop().toLowerCase() === 'pdf') {
                console.log(`Deleting file: ${file}`);
                fs.unlinkSync(path + file)
              }
            });
          console.log("Deleted all the pdf files")
          return true;
        } catch (err) {
          console.error("Error in deleting files",err);
        }
    }

    document.getElementById('downloadbutton').onclick = deletepdf()

    </script>

This is my JavaScript script but it doesn't seem to work at all when I click the button which has the ID 'downloadbutton', I'm using Flask and I also tried this alternative

    os.remove(f"C:/Users/Arshia's PC/PycharmProjects/Flask/static/" + pdfname)

and this works but the only problem is that this deletes the file I need before it is uploaded to the static folder so it cannot be uploaded in time, so the only logical solution is to delete the file when the button is pressed as that is also when it is being uploaded. I'm not sure what's wrong with my JavaScript code and was wondering what I should do to fix it, I thought it might be the path but I tried many different path's and it still didn't work so I'm a bit confused. I got the script from another Stack Overflow post and since I'm using Flask the console logs are useless so that may be what is causing the issue but I'm not entirely sure.

about 4 years ago · Juan Pablo Isaza
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