473,761 Members | 5,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4478
> 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.tt g.co.uk> wrote in message
news:eG******** ******@TK2MSFTN GP09.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.tt g.co.uk> wrote in message
news:eG******** ******@TK2MSFTN GP09.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************ ************@te levox.com> wrote in message
news:eT******** ******@TK2MSFTN GP12.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.tt g.co.uk> wrote in message
news:eG******** ******@TK2MSFTN GP09.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
2141
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" Response.Write "<?xml version='1.0' encoding='UTF-8'?><testing/>" Response.Flush I have a client side ActiveX control implementing IPersistMoniker that is
0
2562
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 and then send it to the user with Response.ContentType and BinaryWrite. Everything works fine until I try to Replace() certain strings in the *.RTF file with my own data. I'm assuming Replace() is choking on the binary characters, or
0
1571
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 IE by default. I search the Internet and found the best way to do it is by using stream. The following is my code for the download.asp. There are 2 problems, one is when I use firefox and click the link to download.asp, I found the system become...
6
14339
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 be the answer) Thanks *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
0
1831
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 am streaming files from a Web Server (.aspx page) using the code bellow. --------------------------------------------------------------
2
1925
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 recognizes the type, showing a blank string or some weird characters. But if I put an extension like .pdf ou .exe to that same file the ContentType shows "text/plain". Is that result expected? ----------------------- Posted by a user from .NET 247...
0
1659
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 retrieve binary data (i.e. downloading files) which is sent by the service using the "SOAP with attachments" format (as per http://www.w3.org/TR/SOAP-attachments), i.e. as a MIME multipart/related message. Apparently, the.NET Webservice library does...
1
1659
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 of a multipart. I have split the MIME into strings which I write to a file via a BinaryWriter. All this of course gives me is exactly the same content as the string. My question is how do I take a string containing MIME and create a file that...
6
6256
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 thought I should. According to what I've read .zip files should return something like 'application/zip' or 'application/x-zip-compressed', RAR should return 'application/x-rar-compressed' but both return 'application/octet-stream'. I'm getting the...
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.