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

253
Visualizações
Using AJAX to call the controller and download a file

I am trying to download a file from the server side. I need to call the function using AJAX. I have tested if the download works without using AJAX, just testing the URL directly in the web browser, it works. But when I try to use AJAX, it executes the function but the response does not work.

CONTROLLER:

@RequestMapping(value = "descargaUsuaris", method = RequestMethod.GET) 
    public final void descargaUsuaris(HttpServletResponse response) throws IOException, ParseException {
        byte[] processedFile = getUsuaris();
         
        JSONObject output;
        try {
            output = new JSONObject(new String(processedFile, StandardCharsets.UTF_8));
            JSONArray docs = output.getJSONArray("data");

            File file = new File("temp/" + "temp_csv.csv");
            File file2 = new File("temp/" + "usuaris.csv");
            

            jsonToCsv(docs, file, file2);

            file.delete();
            ContentDisposition disposition = ContentDisposition.builder( "attachment" )
                    .filename( "taulaMatriculaEstudiant.csv", StandardCharsets.UTF_8 )
                    .build();
                response.setContentType( "text/csv" );
                response.setHeader( "Cache-Control", "no-cache" );
                response.setHeader( "Expires", "0" );
                response.setHeader( "Pragma", "no-cache" );

                OutputStream  out = response.getOutputStream();
                IOUtils.copy( new FileInputStream( file2.getAbsolutePath() ), out  ); // IOUtils from Apache Commons-IO
                out.flush();
                file2.delete();

        } catch (Exception e) {
            e.printStackTrace();
        }
            
            
        

    }

JAVASCRIPT:

function descargaUsuaris(data) {

    var url = "/9avaldoval/administracio/descargaUsuaris";
    var form = $('#formAdmBlue');
    $.ajax({
        url: url,
        type: 'GET',
        data: form.serialize(),
        success: function() {
            
        }


    });

}
over 4 years ago · Santiago Trujillo
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