private async void MenuItem_cs文件还原设计模型_Click_1(object sender, RoutedEventArgs e) { MenuItem item = (MenuItem)sender; ContextMenu menu = (ContextMenu)item.Parent; var obj = (StackPanel)menu.PlacementTarget; ProjectNode projectNode = (ProjectNode)obj.Tag; using (System.Windows.Forms.OpenFileDialog f = new System.Windows.Forms.OpenFileDialog()) { f.Filter = "*.cs|*.cs"; if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.Cursor = Cursors.Wait; try { var bs = System.IO.File.ReadAllBytes(f.FileName); string url = $"POST /ImportCSFileHandler.aspx?projectid={projectNode.Project.id}"; var host = $"{Helper.WebSite}/"; host = host.Substring(host.IndexOf("://") + 3); host = host.Substring(0, host.IndexOf("/")); int port = 80; if (host.Contains(":")) { port = Convert.ToInt32(host.Split(':')[1]); host = host.Split(':')[0]; } string result = null; await Task.Run(() => { Way.Lib.NetStream client = new Way.Lib.NetStream(host, port); client.AsSSLClient(System.Security.Authentication.SslProtocols.None); System.IO.StreamWriter stream = new System.IO.StreamWriter(client); stream.WriteLine(url); stream.WriteLine($"Cookie: WayScriptRemoting={Net.RemotingClient.SessionID}"); stream.WriteLine($"Content-Type: import"); stream.WriteLine($"Content-Length: {bs.Length}"); stream.WriteLine(""); stream.Flush(); client.Write(bs, 0, bs.Length); while (true) { if (client.ReadLine().Length == 0) { break; } } result = client.ReadLine(); client.Close(); }); if (result != "ok") { Helper.ShowError(this, result); return; } DatabaseNode dbnode = (DatabaseNode)projectNode.Children.Where(m => m is TreeNode.DatabaseNode).FirstOrDefault(); dbnode.ReBindItems(); MessageBox.Show(this, "成功导入!"); } catch (Exception ex) { MessageBox.Show(this, ex.GetBaseException().Message); } finally { this.Cursor = null; } } } }
async void import() { using (System.IO.BinaryReader br = new System.IO.BinaryReader(System.IO.File.OpenRead(_filepath))) { string result = null; var tablenames = br.ReadString().ToJsonObject <List <string> >(); int[] rowCounts = br.ReadString().ToJsonObject <int[]>(); var host = $"{Helper.WebSite}/"; host = host.Substring(host.IndexOf("://") + 3); host = host.Substring(0, host.IndexOf("/")); int port = 80; if (host.Contains(":")) { port = Convert.ToInt32(host.Split(':')[1]); host = host.Split(':')[0]; } string url = $"POST /ImportTableData.aspx?dbid={m_databaseItemNode.Database.id}&clearDataFirst={(chkClearDataFirst.IsChecked == true ? 1 : 0)}"; string[] importTables = new string[list.SelectedItems.Count]; for (int i = 0; i < list.SelectedItems.Count; i++) { importTables[i] = list.SelectedItems[i].ToString(); } int totalCount = 0; for (int i = 0; i < importTables.Length; i++) { totalCount += rowCounts[tablenames.IndexOf(importTables[i])]; } await Task.Run(() => { try { Way.Lib.NetStream client = new Way.Lib.NetStream(host, port); client.AsSSLClient(System.Security.Authentication.SslProtocols.None); System.IO.StreamWriter stream = new System.IO.StreamWriter(client); stream.WriteLine(url); stream.WriteLine($"Cookie: WayScriptRemoting={Net.RemotingClient.SessionID}"); stream.WriteLine($"Content-Type: import"); stream.WriteLine(""); stream.Flush(); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(client); bw.Write(importTables.ToJsonString()); int uploaded = 0; while (true) { string tablename = br.ReadString(); if (tablename == ":end") { break; } string content = br.ReadString(); if (importTables.Contains(tablename) == false) { continue; } bw.Write(tablename); bw.Write(content); bw.Flush(); uploaded++; if (totalCount > 0) { this.Dispatcher.Invoke(() => { this.Title = $"正在导入...{(uploaded * 100) / totalCount}%"; }); } } bw.Write(":end"); bw.Flush(); while (true) { if (client.ReadLine().Length == 0) { break; } } result = client.ReadLine(); client.Close(); } catch (Exception ex) { result = ex.Message; } }); if (result != "ok") { this.IsEnabled = true; Helper.ShowError(this, result); } else { Helper.ShowMessage(this, "导入完毕!"); this.Close(); } } }