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

  • 相关阅读:
    SGU 275. To xor or not to xor
    4364: [IOI2014]wall砖墙
    3211: 花神游历各国
    5248: [2018多省省队联测]一双木棋
    3106: [cqoi2013]棋盘游戏
    POJ 1568 Find the Winning Move
    P3527 [POI2011]MET-Meteors
    P2617 Dynamic Rankings
    3262: 陌上花开
    1176: [Balkan2007]Mokia
  • 原文地址:https://www.cnblogs.com/YEKEYISHUO/p/2886358.html
Copyright © 2011-2022 走看看