通过将文本内容以流的形式发送的服务器端,然后再服务器通过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; } }
可运行代码如下: