html - 通过查看将照片和视频保存在数据库中
问题描述
我有一个登录和注销程序。我有一个练习课,我有一个创建视图,允许我创建由名称、照片、视频组成的练习。我在查看创建中填写表格,当我单击创建时出现此错误
NullReferenceException: Object reference not set to an instance of an object.
WebApplication1.Controllers.ExerciciosGinasiosController.Create(ExerciciosGinasio exerciciosGinasio, IFormFile fotografia, IFormFile video) in ExerciciosGinasiosController.cs
string nome_ficheiro = Path.GetFileName(fotografia.FileName);
在我的运动课上
[Table("Exercicios_Ginasio")]
public partial class ExerciciosGinasio
{
public ExerciciosGinasio()
{
Inclui = new HashSet<Inclui>();
}
[Key]
[Column("IDExercicios_Ginasio")]
public int IdexerciciosGinasio { get; set; }
[Required]
[Column("nome")]
[StringLength(30)]
public string Nome { get; set; }
[Required]
[Column("texto_descritivo")]
[StringLength(1000)]
public string TextoDescritivo { get; set; }
[Required]
[Column("foto")]
public string Foto { get; set; }
[Required]
[Column("video")]
public string Video { get; set; }
[InverseProperty("IdexerciciosGinasioNavigation")]
public virtual ICollection<Inclui> Inclui { get; set; }
}
}
在属于练习类(ExercisesController)的控制器中,我有这个方法来创建一个新的练习
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("IdexerciciosGinasio,Nome,TextoDescritivo,Foto,Video")] ExerciciosGinasio exerciciosGinasio, IFormFile fotografia,IFormFile video)
{
string caminho = Path.Combine(_hostEnviroment.ContentRootPath, "wwwroot\\Exercicios");
string nome_ficheiro = Path.GetFileName(fotografia.FileName);
string caminho_completo = Path.Combine(caminho, nome_ficheiro);
FileStream fs = new FileStream(caminho_completo, FileMode.Create);
fotografia.CopyTo(fs);
exerciciosGinasio.Foto = caminho_completo;
fs.Close();
string caminho2 = Path.Combine(_hostEnviroment.ContentRootPath, "wwwroot\\Exercicios");
string nome_ficheiro2 = Path.GetFileName(video.FileName);
string caminho_completo2 = Path.Combine(caminho2, nome_ficheiro2);
FileStream _fs = new FileStream(caminho_completo2, FileMode.Create);
video.CopyTo(_fs);
exerciciosGinasio.Video = caminho_completo2;
_fs.Close();
if (ModelState.IsValid)
{
_context.Add(exerciciosGinasio);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(exerciciosGinasio);
}
鉴于这种方法,我有
@model WebApplication1.Models.ExerciciosGinasio
@{
ViewData["Title"] = "Create";
}
<h4>Criar Exercicio</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Nome" class="control-label"></label>
<input asp-for="Nome" class="form-control" />
<span asp-validation-for="Nome" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="TextoDescritivo" class="control-label"></label>
<input asp-for="TextoDescritivo" type="text" class="form-control" />
<span asp-validation-for="TextoDescritivo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Foto" class="control-label"></label>
<input asp-for="Foto" type="file" class="form-control" accept=".png, .jpg, .bmp" value="" />
@*<span asp-validation-for="Foto" class="text-danger"></span>*@
<div>
<input type="hidden" name="fotografia" value="0" />
</div>
<div class="form-group">
<label asp-for="Video" class="control-label"></label>
<input asp-for="Video" type="file" class="form-control" />
@*<span asp-validation-for="Video" class="text-danger"></span>*@
</div>
<div>
<input type="hidden" name="video" value="0" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<br />
<div>
<a asp-action="Index" asp-controller="Home">Voltar</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
有谁知道可能导致此错误的原因?我该如何解决
解决方案
NullReferenceException:对象引用未设置为对象的实例。
string nome_ficheiro = Path.GetFileName(fotografia.FileName);
在您的视图页面中,我们可以发现您正在使用默认值为 0 的隐藏字段fotografia
和video
,但您的Create操作需要通过IFormFile
对象的文件,如果您调试代码,您会发现fotografia
null ,这会导致异常,而您读取FileName的属性fotografia
。
要修复它,您可以修改如下代码。
照片和视频的输入
<div class="form-group">
<label asp-for="Foto" class="control-label"></label>
<input type="file" class="form-control" name="fotografia" accept=".png, .jpg, .bmp" />
</div>
<div>
<input type="hidden" name="Foto" value="0" />
</div>
<div class="form-group">
<label asp-for="Video" class="control-label"></label>
<input type="file" name="fvideo" class="form-control" />
</div>
<div>
<input type="hidden" name="Video" value="0" />
</div>
控制器动作
[HttpPost]
public IActionResult Create([Bind("IdexerciciosGinasio,Nome,TextoDescritivo,Foto,Video")] ExerciciosGinasio exerciciosGinasio,
IFormFile fotografia,
IFormFile fvideo)
{
string nome_ficheiro = Path.GetFileName(fotografia.FileName);
//code logic here
return View(exerciciosGinasio);
}
测试结果
推荐阅读
- reactjs - 如何从 Firebase 获取一份文档以及如何传递 id 来删除它?
- c++ - 在 C++ 中,使用参数包作为初始化列表是不好的做法吗?
- javascript - 仅模拟模块中的一项功能,但保留原始功能
- rest - Apache Camel Route 没有设置正文
- ios - 您可以自定义 SwiftUI 上下文菜单的激活突出显示颜色吗?
- xcode - 在 Xcode 上构建失败
- python - 如何在 django 中获取唯一值
- javascript - javascript中的 Date(d.getTime()) 和 new Date(d.getTime()) 有什么区别?
- react-native - 带有选项卡导航的 react-native 抽屉导航
- sql - SQL中连续行之间的区别,包括第一行和最后一行?