Silverlight 上传程序
Handler.ashx
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
namespace SilverlightUpFile.Web
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{ //获取上传的数据流
Stream sr = context.Request.InputStream;
try
{
//生成随机的文件名(本DEMO中的上传图片名称也可用参数方法传递过来)
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random rnd = new Random();
string filename = "";
for (int i = 1; i <= 32; i++)
{
filename += chars.Substring(rnd.Next(chars.Length), 1);
}
byte[] buffer = new byte[4096];
int bytesRead = 0;
//将当前数据流写入服务器端文件夹ClientBin下
using (FileStream fs = File.Create(context.Server.MapPath("ClientBin/" + filename + ".jpg"), 4096))
{
while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0)
{
//向文件中写信息
fs.Write(buffer, 0, bytesRead);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("上传成功");
}
catch (Exception e)
{
context.Response.ContentType = "text/plain";
context.Response.Write("上传失败, 错误信息:" + e.Message);
}
finally
{
sr.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
namespace SilverlightUpFile.Web
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{ //获取上传的数据流
Stream sr = context.Request.InputStream;
try
{
//生成随机的文件名(本DEMO中的上传图片名称也可用参数方法传递过来)
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random rnd = new Random();
string filename = "";
for (int i = 1; i <= 32; i++)
{
filename += chars.Substring(rnd.Next(chars.Length), 1);
}
byte[] buffer = new byte[4096];
int bytesRead = 0;
//将当前数据流写入服务器端文件夹ClientBin下
using (FileStream fs = File.Create(context.Server.MapPath("ClientBin/" + filename + ".jpg"), 4096))
{
while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0)
{
//向文件中写信息
fs.Write(buffer, 0, bytesRead);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("上传成功");
}
catch (Exception e)
{
context.Response.ContentType = "text/plain";
context.Response.Write("上传失败, 错误信息:" + e.Message);
}
finally
{
sr.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Code
private void OnUpLoadClick(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog()
{
Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*",
Multiselect = true
};
if ((bool)(openFileDialog.ShowDialog()))
{
fi = openFileDialog.File;
WebClient webclient = new WebClient();
webclient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webclient_OpenWriteCompleted);
webclient.OpenWriteAsync(new Uri("http://localhost:3239/Handler.ashx", UriKind.Absolute), "POST");
}
}
private void OnUpLoadClick(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog()
{
Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*",
Multiselect = true
};
if ((bool)(openFileDialog.ShowDialog()))
{
fi = openFileDialog.File;
WebClient webclient = new WebClient();
webclient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webclient_OpenWriteCompleted);
webclient.OpenWriteAsync(new Uri("http://localhost:3239/Handler.ashx", UriKind.Absolute), "POST");
}
}
Code
<UserControl x:Class="SilverlightUpFile.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Image x:Name="myImage" Grid.Column="1" />
<StackPanel Grid.Row="1" Background="white" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Stretch">
<Button Grid.Row="1"
Grid.Column="0"
Content="选择图片"
Margin="8" Click="OnClick" FontSize="16" Width="100" Height="30"/>
<Button Grid.Row="1"
Grid.Column="2"
Content="上传该图片"
Margin="8" Click="OnUpLoadClick" FontSize="16" Width="100" Height="30" />
</StackPanel>
</Grid>
</UserControl>
<UserControl x:Class="SilverlightUpFile.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Image x:Name="myImage" Grid.Column="1" />
<StackPanel Grid.Row="1" Background="white" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Stretch">
<Button Grid.Row="1"
Grid.Column="0"
Content="选择图片"
Margin="8" Click="OnClick" FontSize="16" Width="100" Height="30"/>
<Button Grid.Row="1"
Grid.Column="2"
Content="上传该图片"
Margin="8" Click="OnUpLoadClick" FontSize="16" Width="100" Height="30" />
</StackPanel>
</Grid>
</UserControl>