Thursday, March 11, 2010

upload image in silverlight

Hai,
Webservice
using System.IO;
using System.Configuration;
using System.Data;
[WebMethod]
public bool Upload(PictureFile picture)
{
FileStream fileStream = null;
BinaryWriter writer = null;
string filePath;

try
{
filePath = HttpContext.Current.Server.MapPath(".") +
ConfigurationManager.AppSettings["PictureUploadDirectory"] +
picture.PictureName;
int i = 0;
while (System.IO.File.Exists(filePath))
{
string ext = System.IO.Path.GetExtension(filePath);
filePath = HttpContext.Current.Server.MapPath(".") +
ConfigurationManager.AppSettings["PictureUploadDirectory"] +
System.IO.Path.GetFileNameWithoutExtension(filePath) + i + ext;
i++;
}

if (picture.PictureName != string.Empty)
{
fileStream = File.Open(filePath, FileMode.Create);
writer = new BinaryWriter(fileStream);
writer.Write(picture.PictureStream);
}

return true;
}
catch (Exception)
{
return false;
}
finally
{
if (fileStream != null)
fileStream.Close();
if (writer != null)
writer.Close();
}
}
public class PictureFile
{
public string PictureName { get; set; }
public byte[] PictureStream { get; set; }
}
Coding

using SilverlightApplicationgeetingcards.ServiceReference1;
private void btneffects_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG files|*.jpg";
if (openFileDialog.ShowDialog() == true)
{
Stream stream = (Stream)openFileDialog.File.OpenRead();//img11.Clip.GetValue(Image.ClipProperty);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
string fileName = openFileDialog.File.Name;//img11.Clip.GetValue(Image.SourceProperty).ToString();

PictureFile pictureFile = new PictureFile();
pictureFile.PictureName = fileName;
pictureFile.PictureStream = bytes;

services.UploadCompleted += new System.EventHandler(uploadpicturecompleted);
services.UploadAsync(pictureFile);
}
public void uploadpicturecompleted(object sender, UploadCompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result)
{ txtcomments.Text = "Upload succeeded (.'.)"; }
else
{ txtcomments.Text = "Upload failed ('.')"; }
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home