微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,buffer.Length)) > 0)
                {
                    fs.Write(buffer,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,buffer.Length)) > 0)
                {
                    serverStream.Write(buffer,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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐