/// <summary> /// 下载文件,下载文件进度在Read的时候可以显示 /// </summary> /// <param name="filename">下载文件名</param> /// <param name="action">进度0-100</param> /// <returns>下载成功后返回存储在本地文件路径</returns> public static string DownLoadFile(string filename, Action <int> action) { ChannelFactory <IFileTransfer> mfileChannelFactory = null; IFileTransfer fileHandlerService = null; try { if (string.IsNullOrEmpty(filename)) { throw new Exception("文件名不为空!"); } string filebufferpath = AppGlobal.AppRootPath + @"filebuffer\"; if (!Directory.Exists(filebufferpath)) { Directory.CreateDirectory(filebufferpath); } mfileChannelFactory = new ChannelFactory <IFileTransfer>("fileendpoint"); fileHandlerService = mfileChannelFactory.CreateChannel(); DownFile df = new DownFile(); if (AppGlobal.cache.Contains("WCFClientID")) { df.clientId = AppGlobal.cache.GetData("WCFClientID").ToString(); } df.DownKey = Guid.NewGuid().ToString(); df.FileName = filename; DownFileResult result = fileHandlerService.DownLoadFile(df); //mfileChannelFactory.Close();//关闭会话 if (result.IsSuccess) { string filepath = filebufferpath + filename; if (File.Exists(filepath)) { File.Delete(filepath); } FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write); int oldprogressnum = 0; decimal progressnum = 0; long bufferlen = 4096; int count = 0; byte[] buffer = new byte[bufferlen]; //设置服务端的下载进度 setDownFileProgress(df.clientId, df.DownKey, (delegate() { return(Convert.ToInt32(Math.Ceiling(progressnum))); })); while ((count = result.FileStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, count); //获取下载进度 getprogress(result.FileSize, bufferlen, ref progressnum); if (oldprogressnum < Convert.ToInt32(Math.Ceiling(progressnum))) { oldprogressnum = Convert.ToInt32(Math.Ceiling(progressnum)); //setDownFileProgress(df.clientId, df.DownKey, oldprogressnum);//设置服务端的下载进度 if (action != null) { action(Convert.ToInt32(Math.Ceiling(progressnum))); } } } //清空缓冲区 fs.Flush(); //关闭流 fs.Close(); //System.Threading.Thread.Sleep(200); return(filepath); } else { throw new Exception("下载文件失败!"); } } catch (Exception e) { throw new Exception(e.Message + "\n下载文件失败!"); } finally { if (fileHandlerService != null) { (fileHandlerService as IContextChannel).Close(); } if (mfileChannelFactory != null) { mfileChannelFactory.Close(); } } }
/// <summary> /// 下载文件,有进度显示 /// </summary> /// <param name="filename">下载文件名</param> /// <param name="action">进度0-100</param> /// <returns>下载成功后返回存储在本地文件路径</returns> public string DownLoadFile(string filename, Action <int> action) { IFileTransfer fileHandlerService = null; try { if (string.IsNullOrEmpty(filename)) { throw new Exception("文件名不为空!"); } string filebufferpath = AppRootPath + @"filebuffer\"; if (!Directory.Exists(filebufferpath)) { Directory.CreateDirectory(filebufferpath); } fileHandlerService = mfileChannelFactory.CreateChannel(); DownFile df = new DownFile(); df.clientId = mConn == null ? "" : mConn.ClientID; df.DownKey = Guid.NewGuid().ToString(); df.FileName = filename; DownFileResult result = new DownFileResult(); result = fileHandlerService.DownLoadFile(df); if (result.IsSuccess) { string filepath = filebufferpath + filename; if (File.Exists(filepath)) { File.Delete(filepath); } FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write); int bufferlen = 4096; int count = 0; byte[] buffer = new byte[bufferlen]; if (action != null) { getupdownprogress(result.FileStream, result.FileSize, action);//获取进度条 } while ((count = result.FileStream.Read(buffer, 0, bufferlen)) > 0) { fs.Write(buffer, 0, count); } //清空缓冲区 fs.Flush(); //关闭流 fs.Close(); return(filepath); } else { throw new Exception("下载文件失败!"); } } catch (Exception e) { throw new Exception(e.Message + "\n下载文件失败!"); } finally { if (fileHandlerService != null) { (fileHandlerService as IContextChannel).Abort(); } } }