坐席客户端5.0

FrmDownLoad.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace Lecall
  13. {
  14. public partial class FrmDownLoad : Form
  15. {
  16. public FrmDownLoad()
  17. {
  18. InitializeComponent();
  19. }
  20. public string filename = "";
  21. public string originalurl = "";
  22. private void btnDown_Click(object sender, EventArgs e)
  23. {
  24. DownloadFile(this .Text , @"C:\123.rar");
  25. }
  26. /// <summary>
  27. /// c#,.net 下载文件
  28. /// </summary>
  29. /// <param name="URL">下载文件地址</param>
  30. ///
  31. /// <param name="Filename">下载后的存放地址</param>
  32. /// <param name="Prog">用于显示的进度条</param>
  33. ///
  34. public bool DownloadFile(string URL, string filename)
  35. {
  36. bool downfinished = false;
  37. float percent = 0;
  38. try
  39. {
  40. if (URL.Contains("application/vnd.ms-excel;base64"))
  41. {
  42. string tmpContent = URL;//获取传递上来的文件内容
  43. string contentHead = "data:application/vnd.ms-excel;base64,";
  44. int startIndex = tmpContent.IndexOf(contentHead);
  45. int name_StartIndex = tmpContent.IndexOf(contentHead) + contentHead.Length;
  46. int name_EndIndex = tmpContent.IndexOf('#');
  47. string fileName = filename;// "Excel表格";
  48. if (name_EndIndex != -1)
  49. {
  50. fileName = Uri.UnescapeDataString(tmpContent.Substring(name_StartIndex, name_EndIndex - name_StartIndex));
  51. tmpContent = tmpContent.Substring(name_EndIndex + 1);
  52. }
  53. else
  54. {
  55. tmpContent = tmpContent.Substring(name_StartIndex);
  56. }
  57. byte[] output = Convert.FromBase64String(tmpContent);
  58. //SaveFileDialog dialog = new SaveFileDialog();
  59. //dialog.FileName = fileName + ".xls";
  60. //dialog.Filter = "(Excel文件)|*.xls";
  61. //DialogResult result = dialog.ShowDialog();
  62. //if (result == DialogResult.OK)
  63. //{
  64. using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  65. {
  66. fs.Write(output, 0, output.Length);
  67. fs.Flush();
  68. }
  69. return true;
  70. //}
  71. }
  72. else
  73. {
  74. #region
  75. System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
  76. System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
  77. long totalBytes = myrp.ContentLength;
  78. if (prog != null && totalBytes > 0)
  79. {
  80. prog.Maximum = (int)totalBytes;
  81. }
  82. #region 保存文件对话框
  83. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  84. //设置文件类型
  85. saveFileDialog1.Filter = "*.wav|*.wav";//|*.mp3|*.mp3
  86. //设置文件名称:
  87. saveFileDialog1.FileName = filename;
  88. //设置默认文件类型显示顺序
  89. //saveFileDialog1.FilterIndex = 2;
  90. //保存对话框是否记忆上次打开的目录
  91. saveFileDialog1.RestoreDirectory = true;
  92. //点了保存按钮进入
  93. //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  94. //{
  95. //}
  96. //else
  97. //{
  98. // this.Close();
  99. // return;
  100. //}
  101. #endregion
  102. System.IO.Stream st = myrp.GetResponseStream();
  103. System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
  104. long totalDownloadedByte = 0;
  105. byte[] by = new byte[1024];
  106. int osize = st.Read(by, 0, (int)by.Length);
  107. while (osize > 0)
  108. {
  109. totalDownloadedByte = osize + totalDownloadedByte;
  110. System.Windows.Forms.Application.DoEvents();
  111. so.Write(by, 0, osize);
  112. if (prog != null && totalDownloadedByte > 0)
  113. {
  114. if (prog.Maximum > (int)totalDownloadedByte)
  115. {
  116. prog.Value = (int)totalDownloadedByte;
  117. }
  118. }
  119. osize = st.Read(by, 0, (int)by.Length);
  120. percent = (float)totalDownloadedByte / (float)totalBytes * 100;
  121. System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
  122. }
  123. so.Close();
  124. st.Close();
  125. downfinished = true;
  126. #endregion
  127. }
  128. }
  129. catch (System.Exception)
  130. {
  131. throw;
  132. }
  133. return downfinished;
  134. }
  135. private void FrmDownLoad_Load(object sender, EventArgs e)
  136. {
  137. if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + filename))
  138. {
  139. filename = filename + "(1)";
  140. }
  141. #region
  142. int index = 0;//匹配计数器
  143. string strKey = filename ;//匹配字符串
  144. DirectoryInfo myDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
  145. FileInfo[] _fileList = myDir.GetFiles();
  146. for (int i = 0; i < _fileList.Length; i++)
  147. {
  148. if (_fileList[i].Name.Contains(strKey))
  149. {
  150. Console.WriteLine(_fileList[i].Name);
  151. index++;
  152. }
  153. }
  154. int pindex = filename.IndexOf(".");
  155. if (index > 0)
  156. {
  157. string insertStr= "(" + index.ToString() + ")";
  158. filename = filename.Insert(filename.LastIndexOf('.'), insertStr);
  159. }
  160. #endregion
  161. this.Text = "下载文件---"+filename;
  162. //this.txtPath.Text = @"C:\Users\Administrator\Desktop";
  163. this.txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  164. }
  165. FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
  166. private void btnScan_Click(object sender, EventArgs e)
  167. {
  168. if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  169. {
  170. if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
  171. this.txtPath.Text = this.folderBrowserDialog1.SelectedPath.Trim();
  172. }
  173. }
  174. private void btnDownLoad_Click(object sender, EventArgs e)
  175. {
  176. bool Downl=DownloadFile(originalurl, this.txtPath.Text + @"\" + filename);//Application.StartupPath+@"\"+
  177. if (Downl)
  178. { MessageBox.Show("下载完成!"); }
  179. else
  180. { MessageBox.Show("下载失败!"); }
  181. this.Close();
  182. //this.Close();
  183. }
  184. private void btnCancel_Click(object sender, EventArgs e)
  185. {
  186. this.Close();
  187. }
  188. }
  189. }