1.Silverlight只能显示网站项目目录的ClientBin目录下的图片,也可以在ClientBin目录下建子文件夹进行显示
,例如ClientBin/images目录
2.Silverlight上传图片一种方法是使用WebClient,服务端使用一般处理程序.ashx
参考代码:
*******************************Silverlight客户端代码*****************************************************
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.Windows.Navigation;
using System.Windows.Media.Imaging;
using System.IO;
namespace Silverligh图片相册
{
public partial class AddProduct : Page
{
public string mStrImageFileName = "";
public FileInfo mObjImageFileInfo = null;
private wcfMain.IwcfMainClient client = null;
OpenFileDialog dlg = null;
public AddProduct()
{
InitializeComponent();
client = ServerManager.GetPox();
client.AddProductCompleted += new EventHandler<wcfMain.AddProductCompletedEventArgs>(client_AddProductCompleted);
}
void client_AddProductCompleted(object sender, wcfMain.AddProductCompletedEventArgs e)
{
if (e.Error == null)
{
Dictionary<string, string> dicGet = e.Result;
if (dicGet["Result"] == "OK")
{
MessageBox.Show("添加产品成功!");
mStrImageFileName = "";
mObjImageFileInfo = null;
txtProductName.Text = "";
imgSource.Source = new BitmapImage(
new Uri("", UriKind.Relative));
}
else
{
MessageBox.Show("添加产品失败!" + dicGet["Result"]);
}
}
else
{
MessageBox.Show(e.Error.InnerException.ToString());
}
}
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void btnExit_Click(object sender, RoutedEventArgs e)
{
SessionManager.Session["Login"] = "N";
this.Content = new ShowPicMain();
}
private void btnSelImage_Click(object sender, RoutedEventArgs e)
{
//这里在客户端显示图片,并显示在Image控件中
dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "JPG 图片 (*.jpg)|*.jpg|PNG 图片 (*.png)|*.png";
bool? result = dlg.ShowDialog();
if (result != null && result == true)
{
mStrImageFileName = dlg.File.Name;
mObjImageFileInfo = dlg.File;
BitmapImage image = new BitmapImage();
image.SetSource(dlg.File.OpenRead());
imgSource.Source = image;
dlg.File.OpenRead().Close();
}
}
#region 关键代码,使用WebClient调用一般处理程序,上传图片
private void uploadImage(string fileName, Stream data)
{
Uri uri = new Uri(string.Format("/UploadImageHandler.ashx?filename={0}", fileName), UriKind.Relative);
WebClient client = new WebClient();
client.OpenWriteCompleted += delegate(object s, OpenWriteCompletedEventArgs e)
{
uploadData(data, e.Result);
e.Result.Close();
data.Close();
};
client.OpenWriteAsync(uri);
}
#endregion
private void uploadData(Stream input, Stream output)
{
//这里要注意,传入的输入流不能是关闭的,否则不能成功上传图片
byte[] buffer = new byte[4096];
int bytes;
while ((bytes = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytes);
}
}
private void btnAddProduct_Click(object sender, RoutedEventArgs e)
{
if (txtProductName.Text!="")
{
client.AddProductAsync(txtProductName.Text, "/images/" + mStrImageFileName);
}
else
{
MessageBox.Show("产品名称和图片不能为空!");
}
}
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
if (mStrImageFileName != "" && mObjImageFileInfo != null)
{
uploadImage(mStrImageFileName, mObjImageFileInfo.OpenRead());
MessageBox.Show(string.Format("上传成功!{0}",
mStrImageFileName));
}
}
private void btnReturn_Click(object sender, RoutedEventArgs e)
{
this.Content = new ShowPicMain();
}
}
}
*******************************一般处理程序服务端代码*****************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace Silverligh图片相册.Web
{
/// <summary>
/// UploadImageHandler 的摘要说明
/// </summary>
public class UploadImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//关键代码,把图片存储在网站的ClientBin/images目录下
context.Response.ContentType = "text/plain";
string filename = context.Request.QueryString["filename"].ToString();
string strSaveFileName = context.Server.
MapPath("~/ClientBin/images/" + filename);
if (File.Exists(strSaveFileName))
{
File.Delete(strSaveFileName);
}
using(FileStream fs = File.Create(strSaveFileName))
{
SaveImage(context.Request.InputStream, fs);
}
}
private void SaveImage(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}