void InitLog() { btnWCFClose.Enabled = false; _appParam = AppValue.GetParam(); AppParam.Load(ref _appParam); _appParam._saveDir = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["TransferFilePath"]; var dirPath = "上传地址:" + _appParam._saveDir; info.InitLogMsg("界面开启"); info.InitLogMsg(dirPath); }
public void TransferFile(FileTransferMessage request) { _msgContent = string.Format("开始接收文件,name={0}", request.FileName); //打印日志 _info.InitLogMsg(_msgContent); //文件信息 string uploadFolder = AppValue.GetParam()._saveDir; string savaPath = request.SavePath; string fileName = request.FileName; Stream sourceStream = request.FileData; //判断文件是否可读 if (!sourceStream.CanRead) { throw new Exception("数据流不可读!"); } if (savaPath == null) { savaPath = @"文件传输\"; } if (!savaPath.EndsWith("\\")) { savaPath += "\\"; } if (!uploadFolder.EndsWith("\\")) { uploadFolder += "\\"; } uploadFolder = uploadFolder + savaPath; //创建保存文件夹 if (!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } int fileSize = 0; string filePath = Path.Combine(uploadFolder, fileName);//Combine合并两个路径 try { //文件流传输 FileStream targetStream; using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { //定义文件缓冲区 const int bufferLen = 4096; var buffer = new byte[bufferLen]; int count; while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) { targetStream.Write(buffer, 0, count); fileSize += count; } targetStream.Close(); sourceStream.Close(); } } catch (Exception ex) { //打印日志 _info.InitLogMsg(ex.Message); } //打印日志 _msgContent = string.Format("接收文件完毕 name={0},filesize={1}", request.FileName, fileSize); _info.InitLogMsg(_msgContent); }