我需要通过ajax从服务器下载文件。 问题是该文件未存储在服务器上。 我的基于java的后端自动从请求参数生成文件并在响应正文中返回它:
@RequestMapping(value = "/download", method = RequestMethod.GET) public void download(@RequestParam String description, @RequestParam Long logId, HttpServletResponse response) { try { InputStream fileContent = // getting file as byte stream response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader("Content-Disposition", "attachment; filename=file.zip"); ServletOutputStream responseOutputStream = response.getOutputStream(); org.apache.commons.io.IOUtils.copy(fileContent, responseOutputStream); response.flushBuffer(); } catch (IOException e) { logger.error("Attempt to download file failed", e); } }
所以我需要处理它并允许用户下载文件。 我的客户端包含这个:
$.ajax({ type: "GET", url: "/download", data: { description: "test", logId: 123 }, success: function(data