0

I have written a servlet that will return a csv file (dynamically generated) to the user to download. However the client is not able to see the file size which means they receive no progress indicator.

I have this code

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String csv = getCsv(); resp.setContentType("text/csv"); resp.setContentLength(csv.getBytes().length); resp.getWriter().write(csv); } catch (Exception e) { log.error("error generating feed", e); } } 

thank you.

3
  • What exactly is the question here? Commented Nov 30, 2010 at 20:29
  • 1
    I assume he wants the progress indicator to show the size of the file. Commented Nov 30, 2010 at 20:42
  • Are you applying compression (gzip or so)? Commented Nov 30, 2010 at 20:46

1 Answer 1

1

Filesize is sent in a Response Header, specifically the Content-Length header. You have to build the entire response either in memory or on disk and calculate its size and send the Content-Length header with this size for the client to know how to calculate a progress indicator. You also with some non-standard browsers ( IE ) have to set the Disposition of the file to get the Download file dialog box to pop up and process the response correctly with a progress indicator.

Try using a browser like Firefox and the 'Live HTTP Headers' extension and 'Firebug' to debug what is actually being sent back.

You don't say what browsers this isn't working in so it may be browsers specific and need additional browsers specific meta-data.

Sign up to request clarification or add additional context in comments.

2 Comments

The OP does set the Content-Length: resp.setContentLength(csv.getBytes().length);
that doesn't mean it is being interpreted by the browser which they don't mention correctly, that is why I suggested debugging with Firefox and Live HTTP headers and Firebug.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.