1

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?

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.