/// <summary> /// 下载文件,从升级包中 /// </summary> /// <param name="filedata"></param> /// <param name="ms"></param> /// <returns></returns> private static DownFileResult DownLoadUpgrade(DownFile filedata, ref MemoryStream ms) { DownFileResult result = new DownFileResult(); if (ms == null) { ms = new MemoryStream(); } string path = clientupgradepath + filedata.FileName; if (!File.Exists(path)) { result.IsSuccess = false; result.FileSize = 0; result.Message = "服务器不存在此文件"; result.FileStream = ms; return(result); } FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.CopyTo(ms); ms.Position = 0; //重要,不为0的话,客户端读取有问题 result.IsSuccess = true; result.FileSize = ms.Length; result.FileStream = ms; fs.Flush(); fs.Close(); return(result); }
public DownFileResult DownLoadFile(DownFile downfile) { DownFileResult result = new DownFileResult(); string path = savePath + @"\" + downfile.FileName; if (!File.Exists(path)) { result.IsSuccess = false; result.FileSize = 0; result.Message = "服务器不存在此文件"; result.FileStream = new MemoryStream(); return(result); } Stream ms = new MemoryStream(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.CopyTo(ms); ms.Position = 0; //重要,不为0的话,客户端读取有问题 result.IsSuccess = true; result.FileSize = ms.Length; result.FileStream = ms; fs.Flush(); fs.Close(); //打印日志 _msgContent = string.Format("接收文件完毕 name={0},filesize={1}", downfile.FileName, result.FileSize); _info.InitLogMsg(_msgContent); return(result); }
/// <summary> /// 下载文件 /// </summary> /// <param name="filedata"></param> /// <returns></returns> public static DownFileResult DownLoadFile(DownFile filedata) { try { if (WcfGlobal.IsDebug) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + filedata.clientId + "]准备下载文件..."); } MemoryStream ms = new MemoryStream(); DownFileResult result = new DownFileResult(); if (filedata.FileType == 0)//0:filebuffer目录 1:Upgrade升级包 2:Mongodb { result = DownLoadfilebuffer(filedata, ref ms); } else if (filedata.FileType == 1) { result = DownLoadUpgrade(filedata, ref ms); } else if (filedata.FileType == 2) { result = DownLoadMongodb(filedata, ref ms); } else { result = DownLoadfilebuffer(filedata, ref ms); } if (WcfGlobal.IsDebug) { //获取进度 getupdownprogress(ms, result.FileSize, (delegate(int _num) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + filedata.clientId + "]下载文件进度:%" + _num); })); } if (WcfGlobal.IsDebug) { ShowHostMsg(Color.Green, DateTime.Now, "客户端[" + filedata.clientId + "]下载文件完成"); } //ms.Close(); return(result); } catch (Exception err) { //记录错误日志 EFWCoreLib.CoreFrame.EntLib.ZhyContainer.CreateException().HandleException(err, "HISPolicy"); DownFileResult result = new DownFileResult(); result.IsSuccess = false; return(result); } }
//下载文件 public DownFileResult DownLoadFile(DownFile filedata) { FileStream fs = null; try { if (WcfServerManage.IsDebug) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + filedata.clientId + "]准备下载文件..."); } DownFileResult result = new DownFileResult(); string path = filebufferpath + filedata.FileName; if (!File.Exists(path)) { result.IsSuccess = false; result.FileSize = 0; result.Message = "服务器不存在此文件"; result.FileStream = new MemoryStream(); return(result); } Stream ms = new MemoryStream(); fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.CopyTo(ms); ms.Position = 0; //重要,不为0的话,客户端读取有问题 result.IsSuccess = true; result.FileSize = ms.Length; result.FileStream = ms; fs.Flush(); fs.Close(); if (WcfServerManage.IsDebug) { ShowHostMsg(Color.Green, DateTime.Now, "客户端[" + filedata.clientId + "]下载文件完成"); } return(result); } catch (Exception err) { if (fs != null) { fs.Flush(); fs.Close(); } //记录错误日志 EFWCoreLib.CoreFrame.EntLib.ZhyContainer.CreateException().HandleException(err, "HISPolicy"); DownFileResult result = new DownFileResult(); result.IsSuccess = false; return(result); } }
/// <summary> /// 下载文件,从Mongodb数据库中 /// </summary> /// <param name="filedata"></param> /// <param name="ms"></param> /// <returns></returns> private static DownFileResult DownLoadMongodb(DownFile filedata, ref MemoryStream ms) { DownFileResult result = new DownFileResult(); if (ms == null) { ms = new MemoryStream(); } return(result); }
public void RootDownLoadFile(DownFile downfile, MemoryStream ms, Action <int> action) { if (downfile == null) { throw new Exception("下载文件对象不能为空!"); } if (fileServiceClient == null) { throw new Exception("还没有创建连接!"); } try { DownFileResult result = new DownFileResult(); result = fileServiceClient.RootDownLoadFile(downfile); if (result.IsSuccess) { if (ms == null) { ms = new MemoryStream(); } 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) { ms.Write(buffer, 0, count); } } else { throw new Exception("下载文件失败!"); } } catch (Exception e) { throw new Exception(e.Message + "\n下载文件失败!"); } finally { //if (fileServiceClient != null) //{ // fileServiceClient.Close(); //} } }
/// <summary> /// 下载文件 /// </summary> /// <param name="df">下载文件对象</param> /// <param name="filepath">存放下载文件的路径</param> /// <param name="action">进度条委托</param> public void DownLoadFile(DownFile df, string filepath, Action <int> action) { if (df == null) { throw new Exception("下载文件对象不能为空!"); } try { DownFileResult result = new DownFileResult(); result = fileServiceClient.DownLoadFile(df); if (result.IsSuccess) { 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(); } else { throw new Exception("下载文件失败!"); } } catch (Exception e) { throw new Exception(e.Message + "\n下载文件失败!"); } finally { //if (fileServiceClient != null) //{ // fileServiceClient.Close(); //} } }
/// <summary> /// 从根节点下载文件 /// </summary> /// <param name="downfile"></param> /// <returns></returns> public static DownFileResult RootDownLoadFile(DownFile downfile) { if (WcfGlobal.IsRootMNode) { return(DownLoadFile(downfile)); } else { ShowHostMsg(Color.Green, DateTime.Now, "准备从根节点下载文件..."); DownFileResult dfResult = SuperClient.superClientLink.RootDownLoadFile(downfile); ShowHostMsg(Color.Green, DateTime.Now, "从根节点下载文件完成"); return(dfResult); } }
/// <summary> /// 下载文件服务实现 /// </summary> /// <param name="downfile">表示文件类型</param> /// <returns>文件流</returns> public DownFileResult DownLoadFile(DownFile downfile) { DownFileResult result = new DownFileResult(); //string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\service\" + filedata.FileName; string path = ""; if (downfile.FileType.Equals("cmm", StringComparison.CurrentCultureIgnoreCase)) { //path = PathManager.Instance.ReportsPath; // todo 测试更改目录 path = @"D:\ServerPathRoot\blades\TestPart12\Results\cab5.CMM"; } else if (downfile.FileType.Equals("rpt", StringComparison.CurrentCultureIgnoreCase)) { //path = PathManager.Instance.RptFilePath; // todo 测试更改目录 path = @"D:\ServerPathRoot\blades\TestPart12\Results\propsww.rpt"; } else { path = string.Empty; } if (!File.Exists(path)) { result.IsSuccess = false; result.FileSize = 0; result.Message = "服务器不存在此文件"; result.FileStream = new MemoryStream(); return(result); } Stream ms = new MemoryStream(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.CopyTo(ms); ms.Position = 0; //重要,不为0的话,客户端读取有问题 result.IsSuccess = true; result.FileSize = ms.Length; result.Message = path; // 文件在服务器上的路径 result.FileStream = ms; fs.Flush(); fs.Close(); return(result); }
void DownFile(string filename) { if (_proxy != null) { DownFile dfPath = new DownFile { FileName = filename }; long filesize = 0; bool issuccess = false; string message = ""; Stream filestream = new MemoryStream(); DownFileResult dfresult = _proxy.DownLoadFile(dfPath); filesize = dfresult.FileSize; issuccess = dfresult.IsSuccess; message = dfresult.Message; filestream = dfresult.FileStream; //, out issuccess, out message, out filestream if (issuccess) { if (!Directory.Exists(_savePath)) { Directory.CreateDirectory(_savePath); } byte[] buffer = new byte[filesize]; FileStream fs = new FileStream(_savePath + @"\" + filename, FileMode.Create, FileAccess.Write); int count = 0; while ((count = filestream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, count); } //清空 fs.Flush(); //关闭流 fs.Close(); } else { MessageBox.Show(message); } } }
//下载文件 public DownFileResult DownLoadFile(DownFile filedata) { DownFileResult result = new DownFileResult(); //string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\service\" + filedata.FileName; string path = Path.Combine(ServerDirManager.Inst.ResultDirectory, filedata.FileName); if (!File.Exists(path)) { result.IsSuccess = false; result.FileSize = 0; result.Message = "服务器不存在此文件"; result.FileStream = new MemoryStream(); return(result); } // 判断文件是否创建完成 var t = WaitForRead(path); bool canRead = t.Result; if (!canRead) { result.IsSuccess = false; result.FileSize = 0; result.Message = "报告文件创建超时"; result.FileStream = new MemoryStream(); return(result); } Stream ms = new MemoryStream(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.CopyTo(ms); ms.Position = 0; //重要,不为0的话,客户端读取有问题 result.IsSuccess = true; result.FileSize = ms.Length; result.FileStream = ms; fs.Flush(); fs.Close(); return(result); }
private bool DownFileFromServer(string v) { DownFile df = new DownFile(); df.FileType = v; DownFileResult res = _partConfigService.DownLoadFile(df); if (res.IsSuccess) { //string path = Path.GetDirectoryName(res.Message); // 在客户端建立相同的目录,记录结果信息 // todo 调试修改路径 string path = @"D:\Reports"; _resultRecord.FilePath = path; string filename = Path.Combine(path, Path.GetFileName(res.Message)); if (v.Equals("cmm", StringComparison.CurrentCultureIgnoreCase)) { _resultRecord.CmmFileName = Path.GetFileName(res.Message); } else { _resultRecord.RptFileName = Path.GetFileName(res.Message); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } byte[] buffer = new byte[res.FileSize]; using (FileStream fs = new FileStream(filename /* todo res.Message*/, FileMode.Create, FileAccess.Write)) { int count = 0; while ((count = res.FileStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, count); } fs.Flush(); fs.Close(); } } return(res.IsSuccess); }
/// <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(); } } }