سؤال

I have a very basic question:

I am writing code to download a CSV file using JSP & Servlets. I got the code from internet forums and it is working fine, but I am trying to understand the significance of the two following lines

response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\""
            + filename + "\"");

Now the first line response.setContentType - According to my understanding it tells the browser what kind of data is expected from the server. My question is what does it tell the browser with value application/octet-stream. I see this being used for downloading all kinds of files. If this lines informs the browser that page would download a file, what difference is the next line causing. response.setHeader is having attachment as a parameter.

Can anyone tell me the significance of these 2 lines for file download?

Regards Tarun

هل كانت مفيدة؟

المحلول

The first line describes the datatype.

response.setContentType("APPLICATION/OCTET-STREAM");

APPLICATION/OCTET-STREAM stands for binary data. It may be more precise by specifing the actual filetype. For images it coud be image/png. If the browser knows the exact filetype it may show the file directly.

response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

This says "Don't even try to show the file, just save the file." It also suggests a filename.

update There is a better explaination of mime-types and content-disposition at wikipedia:

http://en.wikipedia.org/wiki/MIME#Content-Disposition

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top