473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

determine the mime/ContentType of a binary stream?

HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in a
SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb users) to
..JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex
Nov 18 '05 #1
4 4458
> The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...
How does your Operating System know what type of file a file is? By the
extension. If the extension is wrong, your Operating System can't identify
the type of file that it is, or do anything useful with it. A binary file is
just a bunch of bits. In other words, you're going to have to use the same
technique. If a user renames a file extension through their own stupidity,
that's their problem.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Alex Vincent" <al**@NOSPAM.ttg.co.uk> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl... HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in a SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb users) to .JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex

Nov 18 '05 #2

How does your Operating System know what type of file a file is? By the
extension.


Not quite true: lots of filetypes have identifying information somewhere at
the start.
For unix (which does not depend on extensions) there are tools that find the
type
of the file by inspecting this information.

Hans Kesting
Nov 18 '05 #3
I've not tried this, but I think you can load the Byte array into a
System.Drawing.Image. Once you have it loaded, check the RawFormat()
Property. I think that will look at the actual byte array and figure out
what kind of image it really is, reguardless of the extension.

Michael
"Alex Vincent" <al**@NOSPAM.ttg.co.uk> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl...
HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in a SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb users) to .JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 12/26/2003
Nov 18 '05 #4
Well, now, I didn't think of that! I have no idea whether or not it would
work, but I'm willing to bet that it would, since these are all graphics
files.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Michael Pearson" <mi************************@televox.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
I've not tried this, but I think you can load the Byte array into a
System.Drawing.Image. Once you have it loaded, check the RawFormat()
Property. I think that will look at the actual byte array and figure out
what kind of image it really is, reguardless of the extension.

Michael
"Alex Vincent" <al**@NOSPAM.ttg.co.uk> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl...
HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in
a
SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here
I can use HttpPostedFile.ContentType to enter into the db the contenttype of the file (because in some cases BMPs are actually renamed (by dumb users) to
.JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a

JPG or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 12/26/2003

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave | last post by:
So, I have some simple asp code to send a response to the client with a particular ContentType defined, code shown below: Response.Clear Response.ContentType = "application/x-custom"...
0
by: Bill | last post by:
I'm using the example shown at : http://support.microsoft.com:80/support/kb/articles/q276/4/88.asp It involves using ASP/VbScript and ADODB.Stream to read binary data from a *.DOC or *.RTF file...
0
by: mizi | last post by:
I am writing an ASP page for downloading some files, such as jpg, doc which are known MIME type. I want to force user save these files through a "file download" dialog instead of open them on the...
6
by: Kaki | last post by:
Given a file, how do I know if it's ascii or unicode or binary? And how do I know if it's rtf or html or etc? In other words, how do I find the stream type or mime type? (No, file extension cannot...
0
by: PIKACHU | last post by:
Hi, I hope someone can help me. I do not usualy write question in newsgroup since I usualy some had the same problem I had. However, I can't find any articles that would solve my problem. I...
2
by: ME via .NET 247 | last post by:
I have a question about how System.Web.HttpPostedFile manages mime types using property ContentType in Windows. When I upload a known "text/plain" file with no extension the property doesnt...
0
by: andreas.baus | last post by:
Hello. I'm trying to build a client using .NET/C# that is capable of communicating with an existing Webservice written in Java using Apache Axis. Now, I've encountered a problem trying to...
1
by: Datawright | last post by:
Apologies if this is a daft question but I'm new to c# and everything is strange to me. I have written a SOAP Extension class that intercepts a web service that returns MIME attachmernts as part...
6
by: Mad Hatter | last post by:
Hi folks I'm a bit confused with an upload script that I've written. I want to be able to check the file type of an upload by checking the mime type but I'm not getting the results that I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.