Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
Usando AJAX para llamar al controlador y descargar un archivo

Estoy tratando de descargar un archivo desde el lado del servidor. Necesito llamar a la función usando AJAX. He probado si la descarga funciona sin usar AJAX, solo probando la URL directamente en el navegador web, funciona. Pero cuando trato de usar AJAX, ejecuta la función pero la respuesta no funciona.

CONTROLADOR:

 @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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda