public ActionResult CreateFile(CreateFileParamDto vm) { if (this.UserInfo.File.OpenType != FileOpenType.None) { return(Failure("当前已打开其它文件,请先关闭其它文件!")); } if (vm != null && !string.IsNullOrEmpty(vm.Name) && vm.CreationTime != DateTime.MinValue && vm.LastWriteTime != DateTime.MinValue) { FileDataParamDto fileDataParam = null; var fileService = this.GetService <IFileService>(); if (fileService.CreateFile(vm, out fileDataParam)) { return(Success(fileDataParam)); } } return(Error()); }
public virtual bool SendFile(string localFullFileName, string remoteDirectory, string remoteName, FilePositionCallback call = null) { bool result = false; try { if (!string.IsNullOrEmpty(localFullFileName) && !string.IsNullOrEmpty(remoteName) && File.Exists(localFullFileName) && this.Client.IsLogin) { var localinfo = new FileInfo(localFullFileName); this.OnFilePositionCallback(call, localinfo.Length, 0); if (this.Client.Host == "127.0.0.1" || this.Client.Host == "localhost") { result = this.AddFileInfo(localinfo, remoteDirectory, remoteName); this.OnFilePositionCallback(call, localinfo.Length, localinfo.Length); } else { using (var fs = File.OpenRead(localFullFileName)) { long fileLength = fs.Length; CreateFileParamDto createDto = new CreateFileParamDto() { Directory = remoteDirectory, Name = remoteName, Length = localinfo.Length, CreationTime = localinfo.CreationTime, LastWriteTime = localinfo.LastAccessTime }; var getDto = this.Client.Request <FileDataParamDto, CreateFileParamDto>(MsgCmd.CreateFile, createDto); FileDataDto dataDto = new FileDataDto(); if (getDto != null) { while (getDto.Index < fileLength) { dataDto.Index = getDto.Index; int size = getDto.Size; long temp = fileLength - dataDto.Index; if (temp < size) { size = Convert.ToInt32(temp); } byte[] buffer = new byte[size]; fs.Seek(dataDto.Index, SeekOrigin.Begin); dataDto.Length = fs.Read(buffer, 0, buffer.Length); dataDto.Data = buffer; getDto = this.Client.Request <FileDataParamDto, FileDataDto>(MsgCmd.UploadFileData, dataDto); bool isContinue = this.OnFilePositionCallback(call, fileLength, dataDto.Index); if (getDto == null || !isContinue) { break; } } this.Client.Request(MsgCmd.CreateFileCompleted); if (getDto != null && getDto.Index >= fileLength) { this.OnFilePositionCallback(call, fileLength, dataDto.Index); result = true; } } } } } } catch (Exception ex) { LogUtils.Error("【FileInfoClient.SendFile】", ex); } return(result); }
public virtual bool CreateFile(CreateFileParamDto vm, out FileDataParamDto fileDataParam) { bool result = false; fileDataParam = null; UserInfo user = SessionUtils.UserInfo; if (user.File.OpenType == FileOpenType.None && !string.IsNullOrEmpty(vm.Name) && vm.CreationTime != DateTime.MinValue && vm.LastWriteTime != DateTime.MinValue) { DateTime createtime = vm.CreationTime; DateTime lasttime = vm.LastWriteTime; if (ConfigUtils.DatabaseType == DatabaseType.Oracle) { createtime = DateTime.Parse(vm.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")); lasttime = DateTime.Parse(vm.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")); } string tempFullName = null; var repository = this.GetRepository <ITempFileRepository>(); var tempDto = repository.Get(vm.Directory, vm.Name); if (tempDto == null || tempDto.Length == 0 || tempDto.Length != vm.Length || tempDto.CreationTime != createtime || tempDto.LastWriteTime != lasttime || !File.Exists(Utils.Combine(ConfigUtils.TempDirectory, tempDto.TempName))) { if (tempDto != null) { repository.Delete(tempDto.Id); tempFullName = Utils.Combine(ConfigUtils.TempDirectory, tempDto.TempName); if (File.Exists(tempFullName)) { try { File.Delete(tempFullName); } catch { } } } tempDto = new TempFileDto() { TempIndex = 0, TempName = Guid.NewGuid().ToString("n") + ".t" }; } FileStream fs = null; tempFullName = Utils.Combine(ConfigUtils.TempDirectory, tempDto.TempName); try { if (File.Exists(tempFullName)) { fs = File.OpenWrite(tempFullName); if (tempDto.TempIndex > 0) { fs.Seek(tempDto.TempIndex, SeekOrigin.Begin); } } else { if (!Directory.Exists(ConfigUtils.TempDirectory)) { Directory.CreateDirectory(ConfigUtils.TempDirectory); } fs = File.Create(tempFullName); if (vm.Length > 0) { fs.SetLength(vm.Length); } } //FileInfoRepository.Instance.Delete(vm.Directory, vm.Name, (int)FileInfoType.File); //string fullName = PathUtils.Combine(ConfigUtils.FileRootPath, user.File.Directory, user.File.Name); //if (File.Exists(fullName)) File.Delete(fullName); user.File.Directory = vm.Directory; user.File.Name = vm.Name; user.File.Length = vm.Length; user.File.CreationTime = vm.CreationTime; user.File.LastWriteTime = vm.LastWriteTime; user.File.Position = fs.Position; user.File.TempName = tempDto.TempName; user.File.Stream = fs; user.File.OpenType = FileOpenType.Write; fileDataParam = new FileDataParamDto() { Index = user.File.Position, Size = ConfigUtils.MAX_FILE_DATA_SIZE }; result = true; string log = string.Format("创建上传文件, Directory: {0}, Name: {1}, Length: {2}, Position: {3}, CreationTime: {4:yyyy-MM-dd HH:mm:ss}, LastWriteTime: {5:yyyy-MM-dd HH:mm:ss}", user.File.Directory, user.File.Name, user.File.Length, user.File.Position, user.File.CreationTime, user.File.LastWriteTime); new OptionLogService().Add(OptionLogType.CreateFile, log); } catch (Exception ex) { try { if (fs != null) { fs.Close(); } if (tempDto.Id == 0) { File.Delete(tempFullName); } } catch { } throw ex; } } return(result); }