Ketone Cops

January 31, 2008

FileUpload control and ContentType

Filed under: ASP.NET 2.0 — delroger @ 10:06 pm
Tags: , , ,
In one of my recent .NET 2.0 web apps, I’d created a page with a file upload facility. In the code-behind page, checking was done for the file extension, file size and content type. Everything seemed to work fine with it until one site user reported that they were unable to upload a Word document. I found out that even though the file extension was being reported correctly, the ContentType of the file was being reported as “application/octet-stream” rather than “application/msword”. Since I’d specifically excluded “application/octet-stream” from permission to be uploaded, the upload failed.

This had me puzzled for quite a while. Even though I could simply have disabled the check for ContentType and relied just on the file extension, I wanted the extra confirmation of file type in code.

I finally managed to re-create the problem the user had reported: I opened a Word document and then tried to upload it while it was still open. In this scenario, the Word document was reported as an “application/octet-stream”; the same thing happens with Excel files, and presumably other Office file types too. All I had to do was add code to tell the user to close their documents: problem solved.

Blog at WordPress.com.