Silverlight 上传程序

 

  Handler.ashx

 

ContractedBlock.gif ExpandedBlockStart.gif 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;
            }
        }
    }
}

 

ContractedBlock.gif ExpandedBlockStart.gif 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");

            }
        }

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值