Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setContentType Vs setHeader

 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I knew using setHeader we can achieve same funcionality as setContentType.

Eg:


response.setContentType("text/html");

is same as


response.setHeader("content-type","text/html");



The doubt I got is while downloading an attachment in a particular format.

If i use
response.setHeader("Content-disposition","attachment;filename=myFilePrint.xls");



The file is getting opened in an excel application. If this is so, then what is the use of content type? When the above setHeader automatically detects the extension of the file name given, if the extension is not provided it opens choose program window defined in my Operating System.

Could you clear me on this issue?

Thanks.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly there are differences between browsers and how they detect file types using the content type and file name. Therefore providing both gives you the best chance that the response will behave as you expect. Omitting on or the other may have a different effect again.

Also, providing a file name is not the preferred mechanism for determining the content type (remember we're talking content type, not necessarily file type) even though IE may tell you otherwise.
 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I believe that I am clear with the doubt which i had.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Content type is for the browser to prepare itself to deal with the content it is about to receive. If you check the headers for a request sent by the browser, you can say that content-type of response corresponds to Accept header of request. Thus, there are default formats with which the browsers can deal without much effort from your side. However, for others, you need to take appropriate care.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic