Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I get the Mime Content Type from a byte array?

238 views
Skip to first unread message

shapper

unread,
Sep 11, 2009, 12:07:12 PM9/11/09
to
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

Alberto Poblacion

unread,
Sep 11, 2009, 12:55:03 PM9/11/09
to
"shapper" <mdm...@gmail.com> wrote in message
news:4ebe753f-63a6-447a...@t2g2000yqn.googlegroups.com...

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.

Peter Duniho

unread,
Sep 11, 2009, 1:13:46 PM9/11/09
to

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.

Miguel

unread,
Aug 8, 2011, 8:33:11 PM8/8/11
to
You can make a string with the array and looking for words
inside
'*********************************GET**EXTENSION**********************************
Dim Ext As String = ""
Dim Extension As String = ""

For Count As Integer = 0 To 25
Dim letter As String = Chr(ByteArrGrid(Count))
Extension = Extension + letter
Next Count
If InStr(Extension, "Jet DB") <> 0 Then
Ext = ".mdb"
ElseIf InStr(Extension, "PDF") <> 0 Then
Ext = ".PDF"
ElseIf InStr(Extension, "JFIF") <> 0 Then
Ext = ".bmp"
ElseIf InStr(Extension, "Exif") <> 0 Then
Ext = ".bmp"
ElseIf InStr(Extension, "PNG") <> 0 Then
Ext = ".bmp"
Else
Dim enc As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
Extension = enc.GetString(ByteArrGrid)
If InStr(Extension, "Office Wordt") <> 0 Then
Ext = ".doc"
ElseIf InStr(Extension, "Word-document") <> 0 Then
Ext = ".doc"
ElseIf InStr(Extension, "Microsoft Excel") <> 0 Then
Ext = ".xls"
Else
Ext = ".txt"
End If

End If
'*********************************************************************************
InputType = InputBox("Shave Document", "DocumentType ?", Ext)

Dim bWrite As BinaryWriter = New BinaryWriter(File.Open(NewFolder & "\" & InputName & InputType, FileMode.Create))
bWrite.Write(ByteArrGrid)
bWrite.Close()
Process.Start("explorer.exe", NewFolder)
Process.Start(NewFolder & "\" & InputName & InputType)


> On Friday, September 11, 2009 12:55 PM Alberto Poblacion wrote:

> 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 have not 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.


>> On Friday, September 11, 2009 1:13 PM Peter Duniho wrote:

>> The MIME type is not 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 are trying to do which approach is best.


>>> On Wednesday, September 16, 2009 12:01 AM shapper wrote:

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

0 new messages