user3451086
user3451086

Reputation: 41

filename getting undefined.zip when content-disposition is set correctly

I am returning a byte array with httpServletResponse and content-disposition header is set correctly. Also the content that I am getting is also correct. But some how the file name is getting undefined.zip.

Below is the code snippet:

        // set content attributes for the response
        response.setContentType("application/octet-stream"); 
        response.setContentLength((int) packageZipFile.length);  
        // set headers for the response
        String headerKey = "Content-Disposition";
        String headerValue = "attachment; filename=\"abc.zip\"";
        response.setHeader(headerKey, headerValue);

        // get output stream of the response
        outStream = response.getOutputStream();
        outStream.write(packageZipFile);  

Also in the REST call response in browser it is set correctly as below. Content-Disposition:attachment; filename="abc.zip"

Any advice on what I am doing wrong. Thanks in advance!

Upvotes: 4

Views: 1226

Answers (1)

seanplwong
seanplwong

Reputation: 1091

I had the same problem, and it turns out I'm visiting localhost and trying to download a file from 192.168.x.x. This behaviour is consistent across chrome and firefox.

This is how I set the headers in node

ctx.response.set('content-type', 'application/zip, application/octet-stream');
ctx.response.set('content-disposition', `attachment; filename="filename-here.zip"`);
ctx.response.set('filename', `filename-here.zip`);

Hope this helps anyone stumble upon this problem after 3 years this question is asked :)

Upvotes: 1

Related Questions