Advertisement
Guest User

Untitled

a guest
Sep 13th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. 1)
  2. public class MediaFile
  3. {
  4. public int Id { get; set; }
  5. public string FileName { get; set; } // Имя файла
  6. public string ContentType { get; set; } // Тип контента (audio/mp3, video/mp4 и т.д.)
  7. public byte[] Content { get; set; } // Содержимое файла
  8. public DateTime UploadDate { get; set; } // Дата загрузки
  9. public string MediaType { get; set; } // Тип медиа (Audio или Video)
  10. }
  11. 2)
  12. public class MediaDbContext : DbContext
  13. {
  14. public MediaDbContext() : base("name=MediaDbContext")
  15. {
  16. }
  17.  
  18. public DbSet<MediaFile> MediaFiles { get; set; }
  19. }
  20. 3)
  21. <connectionStrings>
  22. <add name="MediaDbContext" connectionString="Data Source=YOUR_SERVER_NAME;Initial Catalog=MediaDB;Integrated Security=True" providerName="System.Data.SqlClient" />
  23. </connectionStrings>
  24.  
  25. 4)
  26. public class MediaController : Controller
  27. {
  28. private MediaDbContext db = new MediaDbContext();
  29.  
  30. // GET: Media
  31. public ActionResult Index()
  32. {
  33. var mediaFiles = db.MediaFiles.ToList();
  34. return View(mediaFiles);
  35. }
  36.  
  37. // GET: Media/Upload
  38. public ActionResult Upload()
  39. {
  40. return View();
  41. }
  42.  
  43. // POST: Media/Upload
  44. [HttpPost]
  45. [ValidateAntiForgeryToken]
  46. public ActionResult Upload(HttpPostedFileBase file, string mediaType)
  47. {
  48. if (file != null && file.ContentLength > 0)
  49. {
  50. var mediaFile = new MediaFile
  51. {
  52. FileName = Path.GetFileName(file.FileName),
  53. ContentType = file.ContentType,
  54. MediaType = mediaType,
  55. UploadDate = DateTime.Now
  56. };
  57.  
  58. using (var reader = new BinaryReader(file.InputStream))
  59. {
  60. mediaFile.Content = reader.ReadBytes(file.ContentLength);
  61. }
  62.  
  63. db.MediaFiles.Add(mediaFile);
  64. db.SaveChanges();
  65.  
  66. return RedirectToAction("Index");
  67. }
  68.  
  69. ViewBag.Message = "Пожалуйста, выберите файл для загрузки.";
  70. return View();
  71. }
  72.  
  73. // GET: Media/Play/5
  74. public ActionResult Play(int? id)
  75. {
  76. if (id == null)
  77. {
  78. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  79. }
  80. MediaFile mediaFile = db.MediaFiles.Find(id);
  81. if (mediaFile == null)
  82. {
  83. return HttpNotFound();
  84. }
  85.  
  86. return File(mediaFile.Content, mediaFile.ContentType);
  87. }
  88.  
  89. // GET: Media/Download/5
  90. public ActionResult Download(int? id)
  91. {
  92. if (id == null)
  93. {
  94. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  95. }
  96. MediaFile mediaFile = db.MediaFiles.Find(id);
  97. if (mediaFile == null)
  98. {
  99. return HttpNotFound();
  100. }
  101.  
  102. return File(mediaFile.Content, mediaFile.ContentType, mediaFile.FileName);
  103. }
  104. }
  105.  
  106. 5)
  107. @model IEnumerable<YourNamespace.Models.MediaFile>
  108.  
  109. @{
  110. ViewBag.Title = "Медиафайлы";
  111. }
  112.  
  113. <h2>Медиафайлы</h2>
  114.  
  115. <p>
  116. @Html.ActionLink("Загрузить новый медиафайл", "Upload")
  117. </p>
  118.  
  119. <table class="table">
  120. <tr>
  121. <th>Имя файла</th>
  122. <th>Тип медиа</th>
  123. <th>Дата загрузки</th>
  124. <th>Действия</th>
  125. </tr>
  126. @foreach (var item in Model)
  127. {
  128. <tr>
  129. <td>@item.FileName</td>
  130. <td>@item.MediaType</td>
  131. <td>@item.UploadDate.ToShortDateString()</td>
  132. <td>
  133. @if (item.MediaType == "Video")
  134. {
  135. @Html.ActionLink("Просмотреть", "Play", new { id = item.Id })
  136. }
  137. else
  138. {
  139. @Html.ActionLink("Прослушать", "Play", new { id = item.Id })
  140. }
  141. |
  142. @Html.ActionLink("Скачать", "Download", new { id = item.Id })
  143. </td>
  144. </tr>
  145. }
  146. </table>
  147.  
  148. 6)
  149. public class RouteConfig
  150. {
  151. public static void RegisterRoutes(RouteCollection routes)
  152. {
  153. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  154.  
  155. routes.MapMvcAttributeRoutes(); // Если используете атрибуты маршрутизации
  156.  
  157. routes.MapRoute(
  158. name: "Default",
  159. url: "{controller}/{action}/{id}",
  160. defaults: new { controller = "Media", action = "Index", id = UrlParameter.Optional }
  161. );
  162. }
  163. }
  164. 7)
  165. Шаг 7: Миграции базы данных
  166.  
  167. Используйте Entity Framework Migrations для создания базы данных.
  168.  
  169. Откройте консоль диспетчера пакетов: Tools -> NuGet Package Manager -> Package Manager Console.
  170. Enable-Migrations
  171.  
  172. Add-Migration InitialCreate
  173. Update-Database
  174.  
  175. 8)CREATE DATABASE MediaDB;
  176. GO
  177.  
  178. USE MediaDB;
  179. GO
  180.  
  181. CREATE TABLE MediaFiles (
  182. Id INT PRIMARY KEY IDENTITY(1,1),
  183. FileName NVARCHAR(255) NOT NULL,
  184. ContentType NVARCHAR(100) NOT NULL,
  185. Content VARBINARY(MAX) NOT NULL,
  186. UploadDate DATETIME NOT NULL,
  187. MediaType NVARCHAR(50) NOT NULL
  188. );
  189. GO
  190.  
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement