From: shapper on
Hello,

I am saving an image, either jpg or gif, to a byte and into a
database.
When I get the byte array is there a way to determine the content type
from it?
Or do I need to save in database the content type?

Thanks,
Miguel
From: Alberto Poblacion on
"shapper" <mdmoura(a)gmail.com> wrote in message
news:4ebe753f-63a6-447a-aa13-07b465a02e21(a)t2g2000yqn.googlegroups.com...
> Hello,
>
> I am saving an image, either jpg or gif, to a byte and into a
> database.
> When I get the byte array is there a way to determine the content type
> from it?
> Or do I need to save in database the content type?

If you are only using jpgs or gifs, you can look at the initial bytes of
the array. The gifs always contain 'G', 'I', 'F' in the first three bytes,
and the jpgs also contain some fixed values which I haven't memorized but
should be easy to find.

However, if you are using more kinds of files, there is no universal
mechanism that will automatically find the content-type for every possible
kind of content. In this case, you would do better storing the type in the
database.

From: Peter Duniho on
On Fri, 11 Sep 2009 09:07:12 -0700, shapper <mdmoura(a)gmail.com> wrote:

> I am saving an image, either jpg or gif, to a byte and into a database.
> When I get the byte array is there a way to determine the content type
> from it?
> Or do I need to save in database the content type?

The MIME type isn't inherent in the byte stream. You need to save that
meta-data yourself if you want to be able to recover it reliably.

Of course, an alternative is to write some code that tries all the
different MIME types you expect to be able to handle, and see which one
works. Just depends on what you're trying to do which approach is best.