Skip to main content

Microsoft Silverlight

Answered Question Upload an image file and save it to SQL Server (Without using aspx file)RSS Feed

(0)

cris168
cris168

Member

Member

10 points

32 Posts

Upload an image file and save it to SQL Server (Without using aspx file)

Hello Gurus,

      I hope you can help me on. How to upload an image by using a dialog box and then, save it to SQL Server Database.  I tried to search and never find any solution.

   

Sincerely,

Cris 

 

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Upload an image file and save it to SQL Server (Without using aspx file)

You have to have some logic on the server side (be it aspx or ashx). Something has to accept the upload and write it to the database. The uploader I like best is Silverlight File Upload. You could easily customize the FileUpdate.ashx.cs to write the file to the database instead of a file.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

cris168
cris168

Member

Member

10 points

32 Posts

Re: Re: Upload an image file and save it to SQL Server (Without using aspx file)

Hello Bryant,

          Below is my initial code to  open a filedialog box and then accept any .jpg file (Just try to read and convert it to a byte array for saving it to the database). After that this line of code will create an error (It means that we can't execute the code to convert into byte array). Do you know why it can't open the file or read (provided we select the correct .jpg file from the dialog box) ? 

FileStream fs = new FileStream(file.File.Name, FileMode.OpenOrCreate, FileAccess.Read);


 

 


 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.IO;

using System.Xml.Serialization;

using System.IO.IsolatedStorage;

 

   

OpenFileDialog file = new OpenFileDialog();

file.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";

file.Multiselect = false;

if ((bool)file.ShowDialog())

{

 

FileStream fs = new FileStream(file.File.Name, FileMode.OpenOrCreate, FileAccess.Read); // this line of code will create an ERROR...

byte[] byteImage = new byte[fs.Length];

fs.Read(byteImage, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

}

 

Thanks,
Cris

 

cris168
cris168

Member

Member

10 points

32 Posts

Re: Re: Re: Upload an image file and save it to SQL Server (Without using aspx file)

Follow up..

     Please try this code in the code behind of .xaml file. Not in the Windows Form.

 

 

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Answered Question

Re: Re: Upload an image file and save it to SQL Server (Without using aspx file)

cris168:
FileStream fs = new FileStream(file.File.Name, FileMode.OpenOrCreate, FileAccess.Read); // this line of code will create an ERROR...

You're close, but you don't create a new FileStream, this is the correct pattern...

OpenFileDialog ofd = new OpenFileDialog();

ofd.ShowDialog();

FileStream fs = ofd.File.OpenRead();

MarkBetz
MarkBetz

Participant

Participant

1142 points

211 Posts

Answered Question

Re: Re: Re: Upload an image file and save it to SQL Server (Without using aspx file)

 According to the MSDN documentation for the FileStream ctor:

This member has a SecurityCriticalAttribute attribute, which restricts it to internal use by the .NET Framework for Silverlight class library. Application code that uses this member throws a MethodAccessException.

So, I think what you have to do is call FileInfo.OpenRead() to get a stream back. You get the FileInfo from the OpenFileDialog's properties.

Edit: Jack beat me to it. :)

cris168
cris168

Member

Member

10 points

32 Posts

Re: Re: Re: Re: Upload an image file and save it to SQL Server (Without using aspx file)

For the three Gurus who help me.

 

Thanks a lot it works now.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities