SilverLight中 使用WebClient上传文件

SilverLight中 使用WebClient上传文件,第1张

概述<%@ WebHandler Language="C#"  Class="UploadHandler" %> using System; using System.Web; using System.IO; public class UploadHandler : System.Web.IHttpHandler {     public void ProcessRequest (HttpConte

<%@ 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加上↓

 

  <httpRuntime maxRequestLength="51200"/>  

 

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

<?xml version="1.0"?>   <!DOCTYPE cross-domain-policy SYstem "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">   <cross-domain-policy>     <allow-http-request-headers-from domain="*" headers="*"/>   </cross-domain-policy> 总结

以上是内存溢出为你收集整理的SilverLight中 使用WebClient上传文件全部内容,希望文章能够帮你解决SilverLight中 使用WebClient上传文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1209241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存