Asp.Net 核心 2 - 来自和来自数据库的音频文件
Asp.Net Core 2 - audio files from and to the database
我目前正在创建一个用于播放声音文件的 Web 应用程序。
我遇到的第一个问题是,当将音频文件写入数据库时,所有记录都具有相同的值,即使它们是不同的音频文件,database with audio files
我不确定它是否保存得很好,但我想以与图片相同的方式保存
Mp3 mp3 = new Mp3();
using (var memoryStream = new MemoryStream())
{
await createViewModel.Name_mp3.CopyToAsync(memoryStream);
mp3.Name_mp3 = memoryStream.ToArray();
}
_context.Mp3.Add(mp3);
_context.SaveChanges();
请给我一个提示,看看它是否应该像这样
第二个问题是如何从数据库中提取这些音频文件。我也尝试了从数据库中提取图片的方式。
所以我的观点是这样的
@model IEnumerable<inz.Models.Song>
@{
ViewData["Title"] = "Index";
}
<div class="panelDiv textColor">
<form asp-controller="Songs" asp-action="Index" method="get">
<div class="input-group w-50 m-4 mx-auto">
<input type="text" class="form-control input-lg border-danger searchBorder" placeholder="Wyszukaj utwór lub artyste" name="search" />
<span class="input-group-btn">
<button class="btn btn-danger" type="submit">
<i class="fas fa-search"></i>
</button>
</span>
<div class="dropdown w-0">
<button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
<div class="dropdown-menu bg-danger" aria-labelledby="dropdownMenuButton">
<input type="submit" name="all" value="Wyświetl wszystko" class="btn btn-danger" />
</div>
</div>
</div>
</form>
<div class="text-center m-5">
<a asp-action=Create>
<buton class="textColor btnDiv">
Dodaj nowy utwór <i class="fas fa-plus-circle fa-lg"></i>
</buton>
</a>
</div>
@if (ViewBag.ShowList == true)
{
<div class="table-responsive">
<table class="table tableSong">
<thead class="bg-danger table-borderless">
<tr>
<th>
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Album.Name_Album)
</th>
<th>
@Html.DisplayNameFor(model => model.Artist.Name_Artist)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<i class="fas fa-thumbs-up"></i>
<i class="fas fa-thumbs-down"></i>
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@if (item.Album.Name_Album != null)
{
<a asp-controller="Songs" asp-action="Album" asp-route-nameAlbum="@item.Album.Name_Album" class="link">@Html.DisplayFor(modelItem => item.Album.Name_Album)</a>
}
else
{
<span>Brak informacji</span>
}
</td>
<td>
<a asp-controller="Songs" asp-action="Artist" asp-route-name="@item.Artist.Name_Artist" class="link">@Html.DisplayFor(modelItem => item.Artist.Name_Artist)</a>
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID_Song">
<buton class="textColor btnIndex">Edytuj</buton>
</a>
<a asp-action="Details" asp-route-id="@item.ID_Song">
<buton class="textColor btnIndex">Detale</buton>
</a>
@if (User.IsInRole("Admin"))
{
<a asp-action="Delete" asp-route-id="@item.ID_Song">
<buton class="textColor btnIndex">Usuń</buton>
</a>
}
</td>
**<td>
@{
var base64 = Convert.ToBase64String(item.Mp3.Name_mp3);
var imgSrc = String.Format("data:audio/mp3;base64,{0}", base64);
}
<audio controls>
<source src="@imgSrc" type="audio/ogg" />
</audio>
</td>**
</tr>
}
</tbody>
</table>
</div>
}
else
{
<p class="mt-5"> Brak zawartości do wyświetlenia</p>
}
</div>
但我还有一个error
我会添加我的模型的样子
public class Mp3
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID_Mp3 { get; set; }
public byte[] Name_mp3 { get; set; }
}
和
public class CreateViewModel
{
public int ID_Song { get; set; }
[Required(ErrorMessage = "Proszę wpisać tytuł")]
[Display(Name = "Tytuł")]
public string Title { get; set; }
[Required(ErrorMessage = "Proszę wpisać nazwę artysty")]
[Display(Name = "Artysta")]
public string Name_Artist { get; set; }
[Display(Name = "Album")]
public string Name_Album { get; set; }
[Display(Name = "Producent")]
public string Name_Producer { get; set; }
public IFormFile Name_mp3 { get; set; }
}
我正在寻求提示,我为我的英语道歉
我不认为将音频文件存储在数据库中是个好主意。
您可以改为将文件保存在磁盘上,然后保存路径。
public class File
{
[Key]
public Guid Id { get; set; }
[Required]
public string Name{ get; set; }
[Required]
public string Path { get; set; }
[Required]
public DateTime Registered{ get; set; }
[Required]
public string RegisteredBy { get; set; }
public string Notes { get; set; }
}
保存文件:
using (var db = new EFModel())
{
if (file.Id == Guid.Empty)
{
file.Id = Guid.NewGuid();
db.Entry(file).State = EntityState.Added;
}
else
db.Entry(file).State = EntityState.Modified;
db.SaveChanges();
return archivo;
}
在保存button/action:
if (!fuArchivo.HasFile)
throw new UserException("Debes seleccionar un archivo.");
string nombreArchivo = Path.GetFileNameWithoutExtension(fuArchivo.FileName);
string extension = Path.GetExtension(fuArchivo.FileName);
string pathArchivo = Path.Combine(ConfigurationManager.AppSettings["rutaCarga"],
ae.IdTitulo.ToString(), ae.IdArchivoEtapa.ToString());
if (!Directory.Exists(pathArchivo))
Directory.CreateDirectory(pathArchivo);
pathArchivo = Path.Combine(pathArchivo, Guid.NewGuid().ToString() + extension);
fuArchivo.SaveAs(pathArchivo);
if (File.Exists(pathArchivo))
{
var archivo = new File()
{
Id = Guid.Empty,
RegisteredBy = ClaimsPrincipal.Current.Identity.Name,
Registered = DateTime.Now,
Name = nombreArchivo,
Path = pathArchivo
};
var on = new FileBO();
return on.Save(archivo);
}
else
throw new Exception("Se guardó el archivo pero no existe.");
抱歉,这是西班牙语。希望这有帮助。
我目前正在创建一个用于播放声音文件的 Web 应用程序。 我遇到的第一个问题是,当将音频文件写入数据库时,所有记录都具有相同的值,即使它们是不同的音频文件,database with audio files
我不确定它是否保存得很好,但我想以与图片相同的方式保存
Mp3 mp3 = new Mp3();
using (var memoryStream = new MemoryStream())
{
await createViewModel.Name_mp3.CopyToAsync(memoryStream);
mp3.Name_mp3 = memoryStream.ToArray();
}
_context.Mp3.Add(mp3);
_context.SaveChanges();
请给我一个提示,看看它是否应该像这样
第二个问题是如何从数据库中提取这些音频文件。我也尝试了从数据库中提取图片的方式。
所以我的观点是这样的
@model IEnumerable<inz.Models.Song>
@{
ViewData["Title"] = "Index";
}
<div class="panelDiv textColor">
<form asp-controller="Songs" asp-action="Index" method="get">
<div class="input-group w-50 m-4 mx-auto">
<input type="text" class="form-control input-lg border-danger searchBorder" placeholder="Wyszukaj utwór lub artyste" name="search" />
<span class="input-group-btn">
<button class="btn btn-danger" type="submit">
<i class="fas fa-search"></i>
</button>
</span>
<div class="dropdown w-0">
<button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
<div class="dropdown-menu bg-danger" aria-labelledby="dropdownMenuButton">
<input type="submit" name="all" value="Wyświetl wszystko" class="btn btn-danger" />
</div>
</div>
</div>
</form>
<div class="text-center m-5">
<a asp-action=Create>
<buton class="textColor btnDiv">
Dodaj nowy utwór <i class="fas fa-plus-circle fa-lg"></i>
</buton>
</a>
</div>
@if (ViewBag.ShowList == true)
{
<div class="table-responsive">
<table class="table tableSong">
<thead class="bg-danger table-borderless">
<tr>
<th>
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Album.Name_Album)
</th>
<th>
@Html.DisplayNameFor(model => model.Artist.Name_Artist)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<i class="fas fa-thumbs-up"></i>
<i class="fas fa-thumbs-down"></i>
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@if (item.Album.Name_Album != null)
{
<a asp-controller="Songs" asp-action="Album" asp-route-nameAlbum="@item.Album.Name_Album" class="link">@Html.DisplayFor(modelItem => item.Album.Name_Album)</a>
}
else
{
<span>Brak informacji</span>
}
</td>
<td>
<a asp-controller="Songs" asp-action="Artist" asp-route-name="@item.Artist.Name_Artist" class="link">@Html.DisplayFor(modelItem => item.Artist.Name_Artist)</a>
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID_Song">
<buton class="textColor btnIndex">Edytuj</buton>
</a>
<a asp-action="Details" asp-route-id="@item.ID_Song">
<buton class="textColor btnIndex">Detale</buton>
</a>
@if (User.IsInRole("Admin"))
{
<a asp-action="Delete" asp-route-id="@item.ID_Song">
<buton class="textColor btnIndex">Usuń</buton>
</a>
}
</td>
**<td>
@{
var base64 = Convert.ToBase64String(item.Mp3.Name_mp3);
var imgSrc = String.Format("data:audio/mp3;base64,{0}", base64);
}
<audio controls>
<source src="@imgSrc" type="audio/ogg" />
</audio>
</td>**
</tr>
}
</tbody>
</table>
</div>
}
else
{
<p class="mt-5"> Brak zawartości do wyświetlenia</p>
}
</div>
但我还有一个error
我会添加我的模型的样子
public class Mp3
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID_Mp3 { get; set; }
public byte[] Name_mp3 { get; set; }
}
和
public class CreateViewModel
{
public int ID_Song { get; set; }
[Required(ErrorMessage = "Proszę wpisać tytuł")]
[Display(Name = "Tytuł")]
public string Title { get; set; }
[Required(ErrorMessage = "Proszę wpisać nazwę artysty")]
[Display(Name = "Artysta")]
public string Name_Artist { get; set; }
[Display(Name = "Album")]
public string Name_Album { get; set; }
[Display(Name = "Producent")]
public string Name_Producer { get; set; }
public IFormFile Name_mp3 { get; set; }
}
我正在寻求提示,我为我的英语道歉
我不认为将音频文件存储在数据库中是个好主意。
您可以改为将文件保存在磁盘上,然后保存路径。
public class File
{
[Key]
public Guid Id { get; set; }
[Required]
public string Name{ get; set; }
[Required]
public string Path { get; set; }
[Required]
public DateTime Registered{ get; set; }
[Required]
public string RegisteredBy { get; set; }
public string Notes { get; set; }
}
保存文件:
using (var db = new EFModel())
{
if (file.Id == Guid.Empty)
{
file.Id = Guid.NewGuid();
db.Entry(file).State = EntityState.Added;
}
else
db.Entry(file).State = EntityState.Modified;
db.SaveChanges();
return archivo;
}
在保存button/action:
if (!fuArchivo.HasFile)
throw new UserException("Debes seleccionar un archivo.");
string nombreArchivo = Path.GetFileNameWithoutExtension(fuArchivo.FileName);
string extension = Path.GetExtension(fuArchivo.FileName);
string pathArchivo = Path.Combine(ConfigurationManager.AppSettings["rutaCarga"],
ae.IdTitulo.ToString(), ae.IdArchivoEtapa.ToString());
if (!Directory.Exists(pathArchivo))
Directory.CreateDirectory(pathArchivo);
pathArchivo = Path.Combine(pathArchivo, Guid.NewGuid().ToString() + extension);
fuArchivo.SaveAs(pathArchivo);
if (File.Exists(pathArchivo))
{
var archivo = new File()
{
Id = Guid.Empty,
RegisteredBy = ClaimsPrincipal.Current.Identity.Name,
Registered = DateTime.Now,
Name = nombreArchivo,
Path = pathArchivo
};
var on = new FileBO();
return on.Save(archivo);
}
else
throw new Exception("Se guardó el archivo pero no existe.");
抱歉,这是西班牙语。希望这有帮助。