public void Upload(FileDataUpload file) { try { Stream stream; bool fileIsFound = _filesUploadStreams.TryGetValue(file.FileId, out stream); if (fileIsFound) { if (file.Data != null) { stream.Write(file.Data, 0, file.Data.Length); } if (file.IsLastPart) { stream.Close(); SaveFileInfoToDataBase(file); _filesUploadStreams.TryRemove(file.FileId, out stream); } } else { string directoryPath = ConfigurationSettings.AppSettings["FileTransferPath"]; DirectoryInfo directoryInfo; if (!Directory.Exists(directoryPath)) { directoryInfo = Directory.CreateDirectory(directoryPath); } else { directoryInfo = new DirectoryInfo(directoryPath); } string filePath = Path.Combine(directoryInfo.FullName, file.FileId); FileStream fsSource = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); fsSource.Write(file.Data, 0, file.Data.Length); if (!file.IsLastPart) { _filesUploadStreams.TryAdd(file.FileId, fsSource); } else { fsSource.Close(); SaveFileInfoToDataBase(file); } } } catch (Exception e) { Console.WriteLine(e); } }
private static void SaveFileInfoToDataBase(FileDataUpload file) { try { FileInfoEx fileInfoEx = new FileInfoEx { FileNameOnServer = file.FileId, OriginalFileName = file.FileName, Author = file.Author, DateUpload = file.DateUpload, Description = file.Description }; using (FileTransferContext context = new FileTransferContext()) { context.Files.Add(fileInfoEx); context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e); } }