Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1)
- public class MediaFile
- {
- public int Id { get; set; }
- public string FileName { get; set; } // Имя файла
- public string ContentType { get; set; } // Тип контента (audio/mp3, video/mp4 и т.д.)
- public byte[] Content { get; set; } // Содержимое файла
- public DateTime UploadDate { get; set; } // Дата загрузки
- public string MediaType { get; set; } // Тип медиа (Audio или Video)
- }
- 2)
- public class MediaDbContext : DbContext
- {
- public MediaDbContext() : base("name=MediaDbContext")
- {
- }
- public DbSet<MediaFile> MediaFiles { get; set; }
- }
- 3)
- <connectionStrings>
- <add name="MediaDbContext" connectionString="Data Source=YOUR_SERVER_NAME;Initial Catalog=MediaDB;Integrated Security=True" providerName="System.Data.SqlClient" />
- </connectionStrings>
- 4)
- public class MediaController : Controller
- {
- private MediaDbContext db = new MediaDbContext();
- // GET: Media
- public ActionResult Index()
- {
- var mediaFiles = db.MediaFiles.ToList();
- return View(mediaFiles);
- }
- // GET: Media/Upload
- public ActionResult Upload()
- {
- return View();
- }
- // POST: Media/Upload
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Upload(HttpPostedFileBase file, string mediaType)
- {
- if (file != null && file.ContentLength > 0)
- {
- var mediaFile = new MediaFile
- {
- FileName = Path.GetFileName(file.FileName),
- ContentType = file.ContentType,
- MediaType = mediaType,
- UploadDate = DateTime.Now
- };
- using (var reader = new BinaryReader(file.InputStream))
- {
- mediaFile.Content = reader.ReadBytes(file.ContentLength);
- }
- db.MediaFiles.Add(mediaFile);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- ViewBag.Message = "Пожалуйста, выберите файл для загрузки.";
- return View();
- }
- // GET: Media/Play/5
- public ActionResult Play(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- MediaFile mediaFile = db.MediaFiles.Find(id);
- if (mediaFile == null)
- {
- return HttpNotFound();
- }
- return File(mediaFile.Content, mediaFile.ContentType);
- }
- // GET: Media/Download/5
- public ActionResult Download(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- MediaFile mediaFile = db.MediaFiles.Find(id);
- if (mediaFile == null)
- {
- return HttpNotFound();
- }
- return File(mediaFile.Content, mediaFile.ContentType, mediaFile.FileName);
- }
- }
- 5)
- @model IEnumerable<YourNamespace.Models.MediaFile>
- @{
- ViewBag.Title = "Медиафайлы";
- }
- <h2>Медиафайлы</h2>
- <p>
- @Html.ActionLink("Загрузить новый медиафайл", "Upload")
- </p>
- <table class="table">
- <tr>
- <th>Имя файла</th>
- <th>Тип медиа</th>
- <th>Дата загрузки</th>
- <th>Действия</th>
- </tr>
- @foreach (var item in Model)
- {
- <tr>
- <td>@item.FileName</td>
- <td>@item.MediaType</td>
- <td>@item.UploadDate.ToShortDateString()</td>
- <td>
- @if (item.MediaType == "Video")
- {
- @Html.ActionLink("Просмотреть", "Play", new { id = item.Id })
- }
- else
- {
- @Html.ActionLink("Прослушать", "Play", new { id = item.Id })
- }
- |
- @Html.ActionLink("Скачать", "Download", new { id = item.Id })
- </td>
- </tr>
- }
- </table>
- 6)
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapMvcAttributeRoutes(); // Если используете атрибуты маршрутизации
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Media", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
- 7)
- Шаг 7: Миграции базы данных
- Используйте Entity Framework Migrations для создания базы данных.
- Откройте консоль диспетчера пакетов: Tools -> NuGet Package Manager -> Package Manager Console.
- Enable-Migrations
- Add-Migration InitialCreate
- Update-Database
- 8)CREATE DATABASE MediaDB;
- GO
- USE MediaDB;
- GO
- CREATE TABLE MediaFiles (
- Id INT PRIMARY KEY IDENTITY(1,1),
- FileName NVARCHAR(255) NOT NULL,
- ContentType NVARCHAR(100) NOT NULL,
- Content VARBINARY(MAX) NOT NULL,
- UploadDate DATETIME NOT NULL,
- MediaType NVARCHAR(50) NOT NULL
- );
- GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement