Wednesday, November 28, 2007

ASP.NET File Upload Content Type variations on CSV Upload

My object was to enable web users using http://www.joinup.ch to import their CSV-exported contacts into the application.

So I implemented a FileUpload via ASP.NET and checked for the content type to distinct between a 'real' excel file that was being uploaded, and a CSV file (exported by Outlook).

The content type for my own CSV file showed up as application/octet-stream, and the import worked nicely (http://www.joinup.ch/ImportInvitees.aspx), while files uploaded by a test user on two other PCs seemed to crash.

Please note that during all the tests, everyone was using Firefox, and not IE.

After some investigation, I found out that my browser/OS sent the ContentType 'application/octet-stream' for the CSV file i was uploading, others were sending
Content type: application/vnd.ms-excel


Now, that seemed quite strange to me. But apparently, depending on the Office/Excel version a user has installed, the content-type changes. Having installed Office (2002) on my own PC, my browser sent 'application/octet-stream' for the CSV text file I uploaded. If there is a newer version installed, the ContentType seems to change to the one mentioned above.

Why Firefox sends a different content-type depending on the Office version puzzles me a littlebit, but this can be handled, once aware.

So if you ever need to upload/import a CSV file to a web application, don't rely on the content type to be the same.

Finally, I kept checking for the content type value, but I also checked for the file extension to make sure to use the right import procedure.

I admit that application/Octet-stream doesn't help much for Content-Type. But the fact that the contenttype value changes depending on the installed application seems a bit scary to me. But then again, most of us have already got used to the fact that MS Software affects many System settings, and this one seems to be one of those. Sad but true.

I will futher investigate on this topic and try to find the definite and clear reason for this.