| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Lecall
- {
- public partial class FrmDownLoad : Form
- {
- public FrmDownLoad()
- {
- InitializeComponent();
- }
- public string filename = "";
- public string originalurl = "";
- private void btnDown_Click(object sender, EventArgs e)
- {
- DownloadFile(this .Text , @"C:\123.rar");
- }
- /// <summary>
- /// c#,.net 下载文件
- /// </summary>
- /// <param name="URL">下载文件地址</param>
- ///
- /// <param name="Filename">下载后的存放地址</param>
- /// <param name="Prog">用于显示的进度条</param>
- ///
- public bool DownloadFile(string URL, string filename)
- {
-
- bool downfinished = false;
- float percent = 0;
- try
- {
- if (URL.Contains("application/vnd.ms-excel;base64"))
- {
- string tmpContent = URL;//获取传递上来的文件内容
- string contentHead = "data:application/vnd.ms-excel;base64,";
- int startIndex = tmpContent.IndexOf(contentHead);
- int name_StartIndex = tmpContent.IndexOf(contentHead) + contentHead.Length;
- int name_EndIndex = tmpContent.IndexOf('#');
-
- string fileName = filename;// "Excel表格";
- if (name_EndIndex != -1)
- {
- fileName = Uri.UnescapeDataString(tmpContent.Substring(name_StartIndex, name_EndIndex - name_StartIndex));
- tmpContent = tmpContent.Substring(name_EndIndex + 1);
- }
- else
- {
- tmpContent = tmpContent.Substring(name_StartIndex);
- }
- byte[] output = Convert.FromBase64String(tmpContent);
- //SaveFileDialog dialog = new SaveFileDialog();
- //dialog.FileName = fileName + ".xls";
- //dialog.Filter = "(Excel文件)|*.xls";
- //DialogResult result = dialog.ShowDialog();
- //if (result == DialogResult.OK)
- //{
- using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- fs.Write(output, 0, output.Length);
- fs.Flush();
- }
- return true;
- //}
- }
- else
- {
-
- #region
- System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
- System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
- long totalBytes = myrp.ContentLength;
- if (prog != null && totalBytes > 0)
- {
- prog.Maximum = (int)totalBytes;
- }
- #region 保存文件对话框
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- //设置文件类型
- saveFileDialog1.Filter = "*.wav|*.wav";//|*.mp3|*.mp3
- //设置文件名称:
- saveFileDialog1.FileName = filename;
- //设置默认文件类型显示顺序
- //saveFileDialog1.FilterIndex = 2;
- //保存对话框是否记忆上次打开的目录
- saveFileDialog1.RestoreDirectory = true;
- //点了保存按钮进入
- //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- //{
- //}
- //else
- //{
- // this.Close();
- // return;
- //}
- #endregion
- System.IO.Stream st = myrp.GetResponseStream();
- System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
- long totalDownloadedByte = 0;
- byte[] by = new byte[1024];
- int osize = st.Read(by, 0, (int)by.Length);
- while (osize > 0)
- {
- totalDownloadedByte = osize + totalDownloadedByte;
- System.Windows.Forms.Application.DoEvents();
- so.Write(by, 0, osize);
- if (prog != null && totalDownloadedByte > 0)
- {
- if (prog.Maximum > (int)totalDownloadedByte)
- {
- prog.Value = (int)totalDownloadedByte;
- }
- }
- osize = st.Read(by, 0, (int)by.Length);
- percent = (float)totalDownloadedByte / (float)totalBytes * 100;
- System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
- }
- so.Close();
- st.Close();
- downfinished = true;
- #endregion
- }
- }
- catch (System.Exception)
- {
- throw;
- }
- return downfinished;
- }
- private void FrmDownLoad_Load(object sender, EventArgs e)
- {
- if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + filename))
- {
- filename = filename + "(1)";
- }
- #region
- int index = 0;//匹配计数器
- string strKey = filename ;//匹配字符串
- DirectoryInfo myDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
- FileInfo[] _fileList = myDir.GetFiles();
- for (int i = 0; i < _fileList.Length; i++)
- {
- if (_fileList[i].Name.Contains(strKey))
- {
- Console.WriteLine(_fileList[i].Name);
- index++;
- }
- }
- int pindex = filename.IndexOf(".");
- if (index > 0)
- {
- string insertStr= "(" + index.ToString() + ")";
- filename = filename.Insert(filename.LastIndexOf('.'), insertStr);
-
- }
- #endregion
- this.Text = "下载文件---"+filename;
- //this.txtPath.Text = @"C:\Users\Administrator\Desktop";
- this.txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- }
- FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
- private void btnScan_Click(object sender, EventArgs e)
- {
-
- if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
- {
- if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
- this.txtPath.Text = this.folderBrowserDialog1.SelectedPath.Trim();
- }
- }
- private void btnDownLoad_Click(object sender, EventArgs e)
- {
- bool Downl=DownloadFile(originalurl, this.txtPath.Text + @"\" + filename);//Application.StartupPath+@"\"+
- if (Downl)
- { MessageBox.Show("下载完成!"); }
- else
- { MessageBox.Show("下载失败!"); }
- this.Close();
- //this.Close();
-
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- }
- }
|