public Document AddDocument(Document newdoc) { var d = _db.Documents.Add(newdoc); _db.SaveChanges(); return d; }
public IHttpActionResult RestoreTpFiles() { List<string> NotFoundedDirs = new List<string>(); string storePath = HttpContext.Current.Server.MapPath(startPath); var dirs = Directory.GetDirectories(storePath); if (dirs!=null) { _docs.ClearDocs(); foreach(var d in dirs) { var di = new DirectoryInfo(d); var tp = _tps.GetTPbyFolderName(di.Name); if (tp!=null) { var files = Directory.GetFiles(storePath+di.Name); if (files!=null) { foreach (var f in files) { var fi = new FileInfo(f); var doc = new Document() {filename = fi.Name,description = fi.Name,tpid = tp.Id }; _docs.AddDocument(doc); } } } else { NotFoundedDirs.Add(di.Name); } } } if (NotFoundedDirs.Count == 0) return Ok(); else return Ok(NotFoundedDirs); }
public async Task<IHttpActionResult> Post(int tp) { var t = _tps.GetTPbyId(tp); if (t != null) { if (Request.Content.IsMimeMultipartContent()) { string uploadPath = HttpContext.Current.Server.MapPath(startPath + Guid.NewGuid().ToString()); string savePath = HttpContext.Current.Server.MapPath(startPath + t.folder.Trim()); MyStreamFileProvider streamProvider = new MyStreamFileProvider(uploadPath); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } await Request.Content.ReadAsMultipartAsync(streamProvider); List<string> messages = new List<string>(); foreach (var file in streamProvider.FileData) { FileInfo fi = new FileInfo(file.LocalFileName); try { File.Copy(fi.FullName, savePath + "/" + fi.Name); var nd = new Document() { filename = fi.Name, description = fi.Name, tpid = t.Id, Id = -1 }; _drs.AddDocument(nd); messages.Add("Загружен файл " + fi.Name + " (" + fi.Length + " байт)"); } catch { messages.Add("Не загружен файл " + fi.Name + " (" + fi.Length + " байт)"); } } Request.Dispose(); Directory.Delete(uploadPath, true); return Ok(messages); } else { return BadRequest(); } } else { return BadRequest("ТП не найдена"); } }