Dear All,
I am working on a silverlight project.In this I need to upload an image and save it in a folder in the server(i.e., in a folder in the application). I want this funtion to be done on button click.
Thanks in advance.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priya LingeRank #785 · 2.9K reputationPosted Aug 11, 2011, 9:22 PM EDT
You can upload images as follow,
On Button_click.
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG files|*.jpg";
//openFileDialog.Filter = "Images (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == true)
{
Stream stream = (Stream)openFileDialog.File.OpenRead();
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
bi = new BitmapImage();
bi.SetSource(stream);
Myimage.Source = bi;
string fileName = openFileDialog.File.Name;
ServiceReference1.ImageFile imagefile = new ServiceReference1.ImageFile();
imagefile.ImageName = fileName;
imagefile.Imagestream = bytes;
//Serive call for saving your images in server folder.
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();(service_UploadCompleted);
service.UploadCompleted += new EventHandler
service.UploadAsync(imagefile);
At service side we have following code to save images in server folder.
public bool Upload(ImageFile image)
{
FileStream fileStream = null;
BinaryWriter writer = null;
string filePath;
try
{
filePath = HttpContext.Current.Server.MapPath(".") + ConfigurationManager.AppSettings["PictureUploadDirectory"] + image.ImageName;
if (image.ImageName != string.Empty)
{
fileStream = File.Open(filePath, FileMode.Create);
writer = new BinaryWriter(fileStream);
writer.Write(image.ImageName);
}
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (fileStream != null)
fileStream.Close();
if (writer != null)
writer.Close();
}
}
You can check images which keeps in pictures folder in server,
D:\\priya\\dotnet\\silverlight applications\\Uploadimages\\Uploadimages.Web/Pictures/pis.jpg.
Please see attached file .
Jiteendra SampathiraoRank #585 · 4.1K reputationPosted Aug 11, 2011, 8:59 PM EDT
hope it will help you......
mahesh112 reputationPosted Aug 11, 2011, 11:36 PM EDT