Mohayemin
Mohayemin

Reputation: 3870

Firefox 13.0.1 cannot correctly determine content type of docx file

My MVC-3 application has a file upload feature. The file content and content type are saved in database so that while trying to download, the correct file can be reencrypted.

Everything is working fine except that if the browser is Firefox 13 (13.0.1 I have), the content type of docx file is detected as text/plain instead of application/vnd.openxmlformats-officedocument.wordprocessingml.document.

I have checked that it works fine in IE. And now I am upgraded to Firefox 14.0.1. Its working fine too.

Now the question is, how can I determine the content type of a HttpPostedFileBase independent of browser?

Upvotes: 0

Views: 892

Answers (2)

Andrey Shchekin
Andrey Shchekin

Reputation: 21599

Original Answer

As I understand it, MIME types are not the most reliable thing.

Personally I would ignore the browser-provided content type completely, and use server map based on FileName extension (not precise either, but at least similar to the experience in OS itself).

After all, client can send you pretty much anything at all as a content type (dependning on the browser and, potentially, system).

Mapping extensions to Content Types

One option (as used by the question author) is to use entries in HKEY_CLASSES_ROOT:

var key = Registry.ClassesRoot.OpenSubKey(extension, false);
var value = key != null ? key.GetValue("Content Type", null) : null;
var mime = value != null ? value.ToString() : string.Empty;

Upvotes: 1

Danny G
Danny G

Reputation: 3769

The web development server may be your issue. You might want to consider testing the solution on IIS and add the mime type to the configuration. The alternative is to override the HTTP response header in the MVC application based off the file extension.

Upvotes: 0

Related Questions