File download using RichFaces

前端 未结 3 1662
渐次进展
渐次进展 2020-12-20 20:59

I got the following to work already:

  1. User can upload a file (i.e. a compressed archive)
  2. User can uncompress the file on the server
  3. User can e
3条回答
  •  醉话见心
    2020-12-20 21:24

    Your concrete problem is that you're attempting to download files by Ajax. This is not correct. JavaScript can't deal with binary responses nor has it any facilities to force a Save As dialogue. You need to make it a normal synchronous request instead so that it's the webbrowser itself who has to deal with it.

    
       
    
    

    As to setting the content type, if you have a file name with extension at your hands, you could use ServletContext#getMimeType() to resolve it based on in web.xml (either the server's default one or your webapp's one).

    ServletContext servletContext = (ServletContext) externalContext.getContext();
    String contentType = servletContext.getMimeType(file.getName());
    
    if (contentType == null) {
        contentType = "application/octet-stream";
    }
    
    response.setContentType(contentType);
    // ...
    

    (note that I assume that you're using JSF 1.x, seeing the way how you obtained the servlet response, you could since JSF 2.x otherwise also use ExternalContext#getMimeType())

提交回复
热议问题