Sorry misread you post - here is what you need
string fileName = "C:mydirmyfile.ext";
Path.GetExtension(fileName);
That will return gif, jpg, or whatever
-----Original Message-----
From: Steven M. Swafford [mailto:steven.swafford gmail.com]
Sent: Sunday, July 22, 2007 7:51 PM
To: aspnet aspadvice.com
Subject: [aspnet] RE: Files in folder to DB upload
How about using the FileInfo object and grabbing the file
extension then using a case statement? You
will most likely need to tweak this a bit.
foreach( FileInfo f in images)
{
int imageLength = Convert.ToInt32(f.Length);
string fileExtension = f.Extension;
string imageType = string.Empty;
switch (fileExtension)
{
case "image/gif":
imageType = "GIF";
break;
case "image/jpeg":
imageType = "JPEG";
break;
}
// Read file into a data stream
byte[] imageData = new Byte[imageLength];
System.IO.Stream imageStream = null;
imageStream.Read(imageData, 0, imageLength);
try
{
SqlCommand cmd = new SqlCommand("insert into Image
"
+ "(Picture, Type) values ( pic, type)", connection);
cmd.Parameters.AddWithValue(" pic", imageData);
cmd.Parameters.AddWithValue(" type", imageType);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
--
Steven M. Swafford
-----Original Message-----
From: Richard [mailto:vze2kxdp optonline.net]
Sent: Friday, July 20, 2007 8:18 PM
To: aspnet aspadvice.com
Subject: [aspnet] RE: Files in folder to DB upload
Thanks again Steven
I am saving and displaying the images quite nicely but I
need one last bit if information from the
file. How do I get it's type (e.g. image/pjpeg). When I
submit from a FileUpload control I am able
to get it from the contentType.
----- Original Message -----
From: "Steven M. Swafford" <steven.swafford gmail.com>
To: <aspnet aspadvice.com>
Sent: Friday, July 20, 2007 7:50 PM
Subject: [aspnet] RE: Files in folder to DB upload
> This is untested but it should give you a jump start..
>
> SqlConnection connection = new
> SqlConnection( "YourConnenctionString");
>
> DirectoryInfo dirInfo = new DirectoryInfo( "C:ImgStore"); FileInfo[]
> images = dirInfo.GetFiles("*.jpg");
>
> connection.Open();
>
> foreach( FileInfo f in images)
> {
> int imageLength = Convert.ToInt32(f.Length);
>
> // Read file into a data stream
> byte[] imageData = new Byte[imageLength];
> System.IO.Stream imageStream = null;
imageStream.Read(imageData, 0,
> imageLength);
>
> try
> {
> SqlCommand cmd = new SqlCommand("insert into Image
"
> + "(Picture) values ( pic)", connection);
> cmd.Parameters.AddWithValue(" pic", imageData); cmd.ExecuteNonQuery();
> }
> catch (Exception ex)
> {
> // handle exception
> }
> }
>
> connection.Close();
> connection.Dispose();
>
> --
> Steven M. Swafford
>
> -----Original Message-----
> From: Richard [mailto:vze2kxdp optonline.net]
> Sent: Friday, July 20, 2007 1:59 PM
> To: aspnet aspadvice.com
> Subject: [aspnet] RE: Files in folder to DB upload
>
> Thanks Steven
> I get all the information but how do I get the actual
file data I need
> to upload to the DB
>
> ----- Original Message -----
> From: "Steven M. Swafford"
<steven.swafford gmail.com>
> To: <aspnet aspadvice.com>
> Sent: Thursday, July 19, 2007 11:24 PM
> Subject: [aspnet] RE: Files in folder to DB upload
>
>
> > Check out the DirectoryInfo Class:
> >
>
http://msdn2.microsoft.com/en-us/library/system.
io.directoryinfo(vs.80).aspx
> >
> > Quick example:
> >
> > DirectoryInfo dirInfo = new DirectoryInfo( "C:ImgStore");
> > FileInfo[] images = dir.GetFiles("*.jpg);
Foreach( FileInfo f in
> > images) {
> > Console.WriteLine("Name is : ",
f.Name);
> > }
> >
> > --
> > Steven M. Swafford
> >
> > -----Original Message-----
> > From: Richard [mailto:vze2kxdp optonline.net]
> > Sent: Thursday, July 19, 2007 9:40 PM
> > To: aspnet aspadvice.com
> > Subject: [aspnet] Files in folder to DB upload
> >
> > Suppose I had a drop folder on my server that I
used ftp to send
> > images
> to.
> > (So I could have a lot of images) Then once a day
or so I want to
> > load
all
> > the files into a database. How would I grab those
files from the
> > folder
> >
> > Thanks
> >
> >
> >
> > --- List Settings ---
> > http://aspadvice.com/list
s/
> >
> >
> > --- List Settings ---
> > http://aspadvice.com/list
s/
> >
> >
> >
> > --
> > No virus found in this incoming message.
> > Checked by AVG Free Edition.
> > Version: 7.5.476 / Virus Database: 269.10.10/908 -
Release Date:
7/19/2007
> 6:10 PM
> >
> >
>
>
>
> --- List Settings ---
> http://aspadvice.com/list
s/
>
>
> --- List Settings ---
> http://aspadvice.com/list
s/
>
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.476 / Virus Database: 269.10.10/908 -
Release Date: 7/19/2007
6:10 PM
>
--- List Settings ---
http://aspadvice.com/list
s/
--- List Settings ---
http://aspadvice.com/list
s/
--- List Settings ---
http://aspadvice.com/list
s/
|