Sally Pepin

Sally Pepin

  • NA
  • 34
  • 3.2k

Display ITEMS from folder - ASP.Net MVC

Aug 4 2017 9:48 AM
Afternoon,
 I wish to know if there's a way I can display in a view the items(files) from a folder inside the application.
 
 
 
-------- or also, need help to How to Download ITEM(file or image) from DB 
 
 
Hello, I need help with the code to download the item or object I keep on my db.
This is my DocumentController and its actions. You can find list for showing, submit, and also details, delete.
  1. using PMPDI_APP.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using PagedList;
  9. using PagedList.Mvc;
  10. using System.Web.Hosting;
  11. using System.IO;
  12. using System.Net;
  13. using System.Data.SqlClient;
  14. namespace PMPDI_APP.Controllers
  15. {
  16. public class DocumentController : Controller
  17. {
  18. PMPDIEntities db = new PMPDIEntities();
  19. // CreateList
  20. public ActionResult List(string document)
  21. {
  22. var docum = db.Documents.Include(p => p.Project);
  23. var a = from p in db.Documents.Include(p => p.Project)
  24. select p;
  25. if (!String.IsNullOrEmpty(document))
  26. {
  27. a = a.Where(
  28. c => c.FileDName.Contains(document)
  29. || c.Project.nameProject.Contains(document)
  30. || c.TypeDocument.typeDocumentName.Contains(document)
  31. );
  32. }
  33. return View(a.ToList());
  34. }
  35. // CreateInsert
  36. public ActionResult submit()
  37. {
  38. ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "nameProject");
  39. ViewBag.typeDocumentID = new SelectList(db.TypeDocuments, "typeDocumentID", "typeDocumentName");
  40. return View();
  41. }
  42. [HttpPost, ValidateAntiForgeryToken]
  43. public ActionResult submit(Document t, HttpPostedFileBase file, [Bind(Include = "DocumentID, Doc, FileDName, typeDocumentID, ProjectID, FileContent, ContentType")]
  44. Document document)
  45. {
  46. if (file != null && file.ContentLength > 0)
  47. {
  48. string ds = file.FileName.Substring(file.FileName.Length - 3);
  49. string p = string.Empty;
  50. p = Server.MapPath("~/Files/");
  51. file.SaveAs(p + file.FileName);
  52. if (file.ContentLength > 0)
  53. {
  54. BinaryReader br = new BinaryReader(file.InputStream);
  55. byte[] buffer = br.ReadBytes(file.ContentLength);
  56. using (db)
  57. {
  58. t.Doc = file.FileName;
  59. t.ContentType = file.ContentType;
  60. t.FileContent = buffer;
  61. db.Documents.Add(t);
  62. db.SaveChanges();
  63. }
  64. }
  65. }
  66. else
  67. {
  68. ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "nameProject");
  69. ViewBag.typeDocumentID = new SelectList(db.TypeDocuments, "typeDocumentID", "typeDocumentName");
  70. return Content("Select File");
  71. }
  72. return RedirectToAction("List");
  73. }
  74. // Details
  75. public ActionResult Details(int id = 0)
  76. {
  77. ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "nameProject");
  78. ViewBag.typeDocumentID = new SelectList(db.TypeDocuments, "typeDocumentID", "typeDocumentName");
  79. return View(db.Documents.Find(id));
  80. }
  81. /*// Update
  82. public ActionResult Edit(int id = 0)
  83. {
  84. return View(db.Documents.Find(id));
  85. }
  86. */
  87. // GET: Projects/Edit/
  88. public ActionResult Edit(int? id)
  89. {
  90. if (id == null)
  91. {
  92. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  93. }
  94. Document document = db.Documents.Find(id);
  95. if (document == null)
  96. {
  97. return HttpNotFound();
  98. }
  99. ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "nameProject");
  100. ViewBag.typeDocumentID = new SelectList(db.TypeDocuments, "typeDocumentID", "typeDocumentName");
  101. return View(document);
  102. }
  103. // POST: Documents/Edit/
  104. [HttpPost, ValidateAntiForgeryToken]
  105. public ActionResult Edit(Document t, HttpPostedFileBase file, [Bind(Include = "DocumentID, Doc, FileDName, typeDocumentID, ProjectID")] Document document)
  106. {
  107. if (file != null)
  108. {
  109. string sd = file.FileName.Substring(file.FileName.Length - 3);
  110. string p = string.Empty;
  111. p = Server.MapPath("~/Files/");
  112. file.SaveAs(p + file.FileName);
  113. using (db)
  114. {
  115. t.Doc = file.FileName;
  116. db.Entry(t).State = EntityState.Modified;
  117. db.SaveChanges();
  118. }
  119. }
  120. ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "nameProject");
  121. ViewBag.typeDocumentID = new SelectList(db.TypeDocuments, "typeDocumentID", "typeDocumentName");
  122. return View(document);
  123. }
  124. public ActionResult Delete(int id = 0)
  125. {
  126. return View(db.Documents.Find(id));
  127. }
  128. [HttpPost, ActionName("Delete")]
  129. public ActionResult delete_conf(int id = 0)
  130. {
  131. Document t = db.Documents.Find(id);
  132. db.Documents.Remove(t);
  133. db.SaveChanges();
  134. return RedirectToAction("List");
  135. }
  136. }
  137. }
If my question is not complete, please, let me know so I can update it.
 

Answers (1)