Browser download complete event

后端 未结 6 1812
自闭症患者
自闭症患者 2020-12-15 21:33

We\'ve been looking for a while for an answer to this, but haven\'t found a solution.

We have a web server, which allows the user to download files (pdfs), which a

6条回答
  •  难免孤独
    2020-12-15 22:19

    You can get a pretty good idea from the server which is connected to the browser directly (the endpoint of TCP connection). The server will get IO error when user cancels download or encounters any network problem. So if you can run the server directly (without proxies). You can do something like this,

      try {
          response.setContentType("application/pdf");
          response.setContentLength(bytes.length);
          ServletOutputStream ouputStream = response.getOutputStream();
          ouputStream.write(bytes, 0, bytes.length);
          ouputStream.flush();
          ouputStream.close();
          logger.info("PDF " + fileName + " sent successfully");
      } catch (Exception e) {
          logger.error("PDF " + fileName + " error: " + e.getMessage());
          throw e;
      }
    

    However, there is still a small chance that the user may not see the PDF in browser after successful download. ACK from browser will be best approach. You can't do that if PDF is displayed by browser directly. You have to use some kind of Javascript PDF viewer and add a call back to server when it's displayed.

提交回复
热议问题