zoukankan      html  css  js  c++  java
  • SL通过handler读写文本文件

    通过将文本内容以流的形式发送的服务器端,然后再服务器通过web handler进行处理

    xaml后台代码:    
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                WebClient wcWrite = new WebClient();
                wcWrite.OpenWriteCompleted += (s, args) =>
                {
                    //将文本内容以流的形式发送的服务器端
                    using (StreamWriter sw = new StreamWriter(args.Result))
                    {
                        sw.Write(textBox1.Text);
                    }
                };
               
                wcWrite.OpenWriteAsync(new Uri(Application.Current.Host.Source, "Handler.ashx"));
            }
    
      handler页面处理:
      public void ProcessRequest(HttpContext context)
            {
                HttpRequest request = context.Request;
                HttpResponse response = context.Response;
    
    
                using (Stream inputStream = request.InputStream)
                {
                    ////获取Client文件夹
                    string folder = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ClientBin"));
                    if (!System.IO.Directory.Exists("ClientBin"))
                        System.IO.Directory.CreateDirectory(folder);
    
    
                    byte[] fileContent = new byte[inputStream.Length];
                    //获取接收到的数据流并写入到fileContent
                    inputStream.Read(fileContent, 0, fileContent.Length);
                    string path = System.Environment.CurrentDirectory + "\\test.log";
                    File.AppendAllText(@"C:\test.log", Encoding.UTF8.GetString(fileContent)+"\r\n");
    
                //    using (FileStream fs = new FileStream(@"C:\test.log", FileMode.Create))
                //    {
                //        byte[] fileContent = new byte[inputStream.Length];
                //        //获取接收到的数据流并写入到fileContent
                //        inputStream.Read(fileContent, 0, fileContent.Length);
                //        //将fileContent写入文件
    
                //        fs.Write(fileContent, 0, fileContent.Length);
                //        fs.Flush();
                         
                //    }
               }
                response.Clear();
                response.End();
    
    
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
    

     可运行代码如下:

    http://download.csdn.net/detail/yekeyishuo/5042596,或者t盘pudn

  • 相关阅读:
    写在“开张”时
    上班真累
    版本控制
    电脑主板报警声音的故障现象对照表
    js页面打开倒计时
    js中的词法分析
    修改mysql数据库密码
    上班的感受
    能力是被逼出来的!!有压力才有动力
    js中绑定事件的三种方式
  • 原文地址:https://www.cnblogs.com/YEKEYISHUO/p/2886358.html
Copyright © 2011-2022 走看看