views:

20

answers:

2

I have a system that's supposed to convert a byte array (not knowing whether its pdf, txt, or doc) provided to me into a downloadable file. Now usually in ASP.NET MVC I would do it like this.

return File(labTestResult.File, "application/pdf");

That's easy because I know what the filetype is. Now that the filetype could be anything, is there a way to download the file to its appropriate format?

+1  A: 

I'm not sure there is a really good way of doing this. Some file formats have "magic numbers" you could check for Look Here for more info on this. I've never tried this myself, so I can't vouch for how solid such an implementation would be. I suppose the best way would be to retrieve the filetype info with the data.

daft
yeah i've been reading about this magic number i guess this would be my last resort unless someone can come up with a very good solution. thanks a lot daft!
Ari
+1  A: 

Asp.net MVC is very extensible, so if you need to send anything differently in the response you can do so i.e. you could write an ActionResult to serve your file without specifying the mime type, but that's really not recommended.

Use the solution in this question to get the mime type from the first bytes.

If you want you can also define a custom action result that uses that code, so then your controller code becomes: return AutoMimeFile(labTestResult.File);

eglasius