SilverLight中 使用WebClient上传文件

<%@ WebHandler Language="C#"  Class="UploadHandler" %>

using System;
using System.Web;
using System.IO;

public class UploadHandler : System.Web.IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {  
         Stream sr = context.Request.InputStream;
         string filename = context.Request.QueryString["fileName"].ToString();
         try
         {
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            
            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);
                }
            }
         }
         catch (Exception e)
         {
         }
         finally
         {
             sr.Dispose();
         }
        
    }
  
    public bool IsReusable {
 get {
            return false;
        }
    } 
}

 

 

 

 

 

 

 

   private void ImportButton_Clicked(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*",
                Multiselect = true
            };

            if (openFileDialog.ShowDialog() == true)//.DialogResult.OK)
            {
                WebClient webclient = new WebClient();

                foreach (FileInfo fi in openFileDialog.Files)
                {
                    webclient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webclient_OpenWriteCompleted);

                    UriBuilder ub = new UriBuilder("http://localhost:52508/UploadHandler.ashx");
                    ub.Query = string.Format("fileName={0}", fi.Name);
                    webclient.OpenWriteAsync(ub.Uri, "POST", fi.OpenRead());

                }
            }
        }

        void webclient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
        {
            System.Net.WebClient client = sender as System.Net.WebClient;

            if (e.Cancelled != true)
            {
                Stream clientStream = e.UserState as Stream;

                Stream serverStream = e.Result;
                byte[] buffer = new byte[4096];
                int count = 0;

                while ((count = clientStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    serverStream.Write(buffer, 0, count);
                }
                serverStream.Close();
                clientStream.Close();
            }
        }

 

 

 

如果上傳超過4MB,記得在Web.config加上↓

 

 
  1. <httpRuntime maxRequestLength="51200"/>  

 

如果不能上傳試試加上crossdomain.xml↓

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">  
  3. <cross-domain-policy>  
  4.   <allow-http-request-headers-from domain="*" headers="*"/>  
  5. </cross-domain-policy>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值