/// <summary> /// 添加文件 /// </summary> /// <param name="fileInfo"></param> /// <param name="fileContent"></param> public void AddFile(string nodePath,DBFileInfo fileInfo, byte[] fileContent) { DiskFile file=DBFileInfo.toDiskFile(fileInfo, fileContent); repository.AddFileOfFolderDB(nodePath, file); //将ID传回 fileInfo.ID = file.ID; }
/// <summary> /// 将DBFileInfo对象转换为EF可以直接保存的DiskFile对象 /// </summary> /// <param name="fileInfo"></param> /// <param name="fileContent"></param> /// <returns></returns> public static DiskFile toDiskFile(DBFileInfo fileInfo, byte[] fileContent) { if (fileInfo != null) { DiskFile file = new DiskFile() { ID = fileInfo.ID, FileSize = fileInfo.FileSize, FilePath = fileInfo.FilePath, AddTime = fileInfo.AddTime, FileContent = fileContent }; return(file); } return(null); }
/// <summary> /// 将DBFileInfo对象转换为EF可以直接保存的DiskFile对象 /// </summary> /// <param name="fileInfo"></param> /// <param name="fileContent"></param> /// <returns></returns> public static DiskFile toDiskFile(DBFileInfo fileInfo, byte[] fileContent) { if (fileInfo != null) { DiskFile file = new DiskFile() { ID = fileInfo.ID, FileSize = fileInfo.FileSize, FilePath = fileInfo.FilePath, AddTime = fileInfo.AddTime, FileContent = fileContent }; return file; } return null; }
private void btnAddFile_Click(object sender, RoutedEventArgs e) { WinForm.OpenFileDialog openFileDialog = new WinForm.OpenFileDialog(); openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog() == WinForm.DialogResult.OK) { foreach (var FileName in openFileDialog.FileNames) { FileInfo fi = new FileInfo(FileName); if (fi.Length > 5 * 1024 * 1024) { MessageBoxResult result = MessageBox.Show("要加入的文件:“"+System.IO.Path.GetFileName(FileName)+"”大小为:"+FileUtils.FileSizeFormater(fi.Length)+",将大文件加入数据库会导致程序性能下降,点击“是”添加此文件,点击“否”跳过此文件", "加入大文件", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); if (result == MessageBoxResult.No) { continue; } } DBFileInfo fileInfo = new DBFileInfo() { AddTime = DateTime.Now, FilePath = FileName, FileSize = fi.Length }; //不加入重复的文件 if (_dataObject.AttachFiles.IndexOf(fileInfo) != -1) { continue; } accessObj.AddFile(_dataObject.Path, fileInfo, System.IO.File.ReadAllBytes(FileName)); _dataObject.AttachFiles.Add(fileInfo); } } }
/// <summary> /// 添加文件 /// </summary> private void AddFiles() { WinForm.OpenFileDialog openFileDialog = new WinForm.OpenFileDialog(); openFileDialog.Multiselect = true; String info = ""; if (openFileDialog.ShowDialog() == WinForm.DialogResult.OK) { foreach (var FileName in openFileDialog.FileNames) { FileInfo fi = new FileInfo(FileName); if (fi.Length > 5 * 1024 * 1024) { MessageBoxResult result = MessageBox.Show("要加入的文件:“" + System.IO.Path.GetFileName(FileName) + "”大小为:" + FileUtils.FileSizeFormater(fi.Length) + ",将大文件加入数据库会导致程序性能下降,点击“是”添加此文件,点击“否”跳过此文件", "加入大文件", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); if (result == MessageBoxResult.No) { continue; } } DBFileInfo fileInfo = new DBFileInfo() { AddTime = DateTime.Now, FilePath = FileName, FileSize = fi.Length }; //不加入重复的文件 if (_dataObject.AttachFiles.IndexOf(fileInfo) != -1) { continue; } //当加入文件时,有可能因为另一进程也使用此文件而导致加载失败 try { accessObj.AddFile(_dataObject.Path, fileInfo, System.IO.File.ReadAllBytes(FileName)); _dataObject.AttachFiles.Add(fileInfo); info = string.Format("正在加入文件{0}", System.IO.Path.GetFileName(FileName)); if (_dataObject.MainWindow != null) { _dataObject.MainWindow.ShowInfo(info); } } catch (IOException ex) { MessageBox.Show(ex.Message); } if (_dataObject.MainWindow != null) { _dataObject.MainWindow.ShowInfo("文件添加完毕"); } } } }