Thursday, February 19, 2015

How to download an excel file with the correct extension

Recently one of my friends asked me about, did you notice any issue with downloading excel files from one of the web applications that we have used?
I said, no actually, since I haven't recently tried it.:D 

And his problem was that, excel files were not downloading with the correct extension in some browsers. So that he cannot open it with MS-EXCEL. I was so sure about that, since it worked for me before. Anyway since he asked I have checked it again.

Oh no, the issue is there too. :o

I have checked with other browsers as well. Oh yeah, it was working fine with IE. And sadly not with firefox. Since I knew that there were no any recent changes with the code and this functionality had worked fine before, I wanted to inspect the elements in the web application. So I have monitored it and found out the fact.

I knew that inorder to set up the file extension for a .xls format in the response header the value for the 'content-type' attribute should set as "Content-Type:application/vnd.ms-excel". And it was Ok. 




And I have checked the content-disposition and I have noticed that the file name is there, but not the extension. So I have added the change and then the application worked fine. I just added the ".xls" extension to the end of the file name for the value of content-disposition attribute in the code level. And this is how it appeared after the changes.



Since this solution worked fine and actually it saved my time and his too, I wanted to share this with others. So I hope this will help for someone else too.!! :)



Cheers
"do good and good will come to you!!"