public ActionResult Upload(int dirId, string fileName, HttpPostedFileBase file) { if (string.IsNullOrEmpty(fileName)) return Json(new { status = "error", message = "A file name is required to upload." }); if (file == null || file.ContentLength == 0) return Json(new { status = "error", message = "A file is required." }); using (IntelliDocsEntities db = new IntelliDocsEntities()) { Directory thisDirectory = db.Directories.Find(dirId); string extension = Path.GetExtension(file.FileName); Document newDoc = new Document() { dirId = dirId, docCreatedDate = DateTime.Now, docName = fileName, docExtension = extension, docContentType = file.ContentType, User_userId = User.Identity.GetUserId() }; db.Documents.Add(newDoc); try { db.SaveChanges(); } catch (Exception ex) { return Json(new { status = "error", message = "Error uploading new Document: " + ex.Message }); } string basePath = Path.Combine(Server.MapPath("~"), thisDirectory.Path); string fullFileName = newDoc.docId + newDoc.docExtension; if (!System.IO.Directory.Exists(basePath)) { System.IO.Directory.CreateDirectory(basePath); } file.SaveAs(Path.Combine(basePath, fullFileName)); } return Json(new { status = "success", message = "The new Document was uploaded successfully." }); }
public ActionResult DeleteDirectory(int id) { using (IntelliDocsEntities db = new IntelliDocsEntities()) { Directory dir = db.Directories.Find(id); if (dir == null) return Json(new { status = "error", message = "Couldn't find the specified Directory." }); if (dir.dirParentId == null) { return Json(new { status = "error", message = "You can't delete your Root directory." }); } string path = dir.Path; db.Entry(dir).State = EntityState.Deleted; try { db.SaveChanges(); } catch (Exception ex) { return Json(new { status = "error", message = "Failed to delete folder: " + ex.Message }); } path = Path.Combine(Server.MapPath("~"), path); System.IO.Directory.Delete(path, true); } return Json(new { status = "success", message = "Folder successfully deleted!" }); }
public ActionResult Delete(int id) { using (IntelliDocsEntities db = new IntelliDocsEntities()) { Document doc = db.Documents.Find(id); string path = doc.Path; db.Entry(doc).State = EntityState.Deleted; try { db.SaveChanges(); } catch (Exception ex) { return Json(new { status = "error", message = "Failed to delete file: " + ex.Message }, JsonRequestBehavior.AllowGet); } System.IO.File.Delete(path); } return Json(new { status = "success", message = "File successfully deleted!" }, JsonRequestBehavior.AllowGet); }