C# 如何在ASP.NETCore2.0Razor页面中发布一个文件?
我在使用ASP.NETCore2.0和Razor页面的应用程序中工作。我一直在关注Microsoft文档中关于如何将文件上载到Azure blob存储的内容,但到目前为止我还无法让它正常工作 我有两个独立的模型类。一个用于文件上传,一个用于Word 文件上载类:C# 如何在ASP.NETCore2.0Razor页面中发布一个文件?,c#,asp.net,asp.net-core,razor-pages,C#,Asp.net,Asp.net Core,Razor Pages,我在使用ASP.NETCore2.0和Razor页面的应用程序中工作。我一直在关注Microsoft文档中关于如何将文件上载到Azure blob存储的内容,但到目前为止我还无法让它正常工作 我有两个独立的模型类。一个用于文件上传,一个用于Word 文件上载类: public class WordUpload { public IFormFile SoundFile { get; set; } public IFormFile SoundFileSentence { get; s
public class WordUpload
{
public IFormFile SoundFile { get; set; }
public IFormFile SoundFileSentence { get; set; }
}
其他类别:
public class Word
{
public int ID { get; set; }
public string Answer { get; set; }
public string AlternativeAnswer { get; set; }
public string Hint { get; set; }
public int Length { get; set; }
public int Vowels { get; set; }
public string Language { get; set; }
public string Category { get; set; }
public int Module { get; set; }
public string Difficulty { get; set; }
public string Sound { get; set; }
public string SoundSentence { get; set; }
}
包含未传递的WordUpload.SoundFile的页面。这就是问题所在,因为它总是返回null
public class CreateModel : PageModel
{
private readonly Data.ApplicationDbContext _context;
private readonly IWordRepository _wordRepository;
public CreateModel(Data.ApplicationDbContext context, IWordRepository wordRepository)
{
_context = context;
_wordRepository = wordRepository;
}
/// <summary>
/// OnGet triggers when the page is opened
/// </summary>
/// <returns></returns>
public IActionResult OnGet()
{
Word = new Word
{
Answer = "",
AlternativeAnswer = "",
Hint = "Hint",
Length = 0,
Vowels = 0,
Language = "DK",
Category = "",
Module = 0,
Difficulty = "",
Sound = "",
SoundSentence = ""
};
return Page();
}
[BindProperty]
public WordUpload WordUpload { get; set; }
[BindProperty]
public Word Word { get; set; }
/// <summary>
/// Posts the data to the database async
/// </summary>
/// <returns></returns>
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
// Get the number of vowels in the word
Word.Vowels = _wordRepository.NumberOfVowels(Word.Answer);
// Save the length
Word.Length = Word.Answer.Length;
// upload file to blob storage
Word.Sound = _wordRepository.UploadAudio(WordUpload.SoundFile);
// save to db
_context.Word.Add(Word);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
公共类CreateModel:PageModel
{
private readonly Data.ApplicationDbContext\u上下文;
专用只读IWordRepository\u wordRepository;
public CreateModel(Data.ApplicationDbContext上下文,IWordRepository wordRepository)
{
_上下文=上下文;
_wordRepository=wordRepository;
}
///
///打开页面时会触发OnGet
///
///
公共IActionResult OnGet()
{
生词
{
答案=”,
备选答案=”,
Hint=“Hint”,
长度=0,
元音=0,
Language=“DK”,
类别=”,
模块=0,
难度=”,
声音=”,
SoundSession=“”
};
返回页();
}
[BindProperty]
public WordUpload WordUpload{get;set;}
[BindProperty]
公共单词{get;set;}
///
///将数据异步发布到数据库
///
///
公共异步任务OnPostAsync()
{
如果(!ModelState.IsValid)
{
返回页();
}
//获取单词中元音的数量
单词.元音=_wordRepository.numberof元音(单词.答案);
//节省时间
Word.Length=Word.Answer.Length;
//将文件上载到blob存储
Word.Sound=\u wordRepository.UploadAudio(WordUpload.SoundFile);
//保存到数据库
_context.Word.Add(Word);
wait_context.SaveChangesAsync();
返回页首(“/索引”);
}
}
以及查看页面:
@page
@model Fabetio.Web.Pages.Words.CreateModel
@{
ViewData["Title"] = "Create";
}
<div class="container-fluid darkblue-background">
<br>
<br>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="task--card">
<div class="dasboard--title">
<h1>@ViewData["Title"]</h1>
<p class="subtitle">Subtitle.</p>
</div>
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Word.ID" />
<div class="form-group">
<label asp-for="Word.Answer" class="control-label"></label>
<input asp-for="Word.Answer" class="form-control" />
<span asp-validation-for="Word.Answer" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Word.AlternativeAnswer" class="control-label"></label>
<input asp-for="Word.AlternativeAnswer" class="form-control" />
<span asp-validation-for="Word.AlternativeAnswer" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Word.Hint" class="control-label"></label>
<input asp-for="Word.Hint" class="form-control" />
<span asp-validation-for="Word.Hint" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Word.Language" class="control-label"></label>
<input asp-for="Word.Language" class="form-control" />
<span asp-validation-for="Word.Language" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Word.Category" class="control-label"></label>
<input asp-for="Word.Category" class="form-control" />
<span asp-validation-for="Word.Category" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Word.Module" class="control-label"></label>
<input asp-for="Word.Module" class="form-control" />
<span asp-validation-for="Word.Module" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Word.Difficulty" class="control-label"></label>
<input asp-for="Word.Difficulty" class="form-control" />
<span asp-validation-for="Word.Difficulty" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="WordUpload.SoundFile" class="control-label"></label>
<input asp-for="WordUpload.SoundFile" type="file" class="form-control" />
<span asp-validation-for="WordUpload.SoundFile" class="text-danger"></span>
</div>
<br>
<br>
<div class="form-group">
<input type="submit" value="Create" class="btn btn--blue" />
</div>
</form>
</div>
</div>
</div>
<br>
<br>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@page
@model Fabetio.Web.Pages.Words.CreateModel
@{
ViewData[“标题”]=“创建”;
}
@ViewData[“标题”]
副标题
@节脚本{
@{wait Html.RenderPartialAsync(“_validationScript”);}
}
WordUpload.SoundFile未随模型一起传递。当我调试应用程序时,它在controller/page中返回为null。传递所有其他属性时不会出现任何问题
您知道如何传递文件吗?您需要在表单上添加以下内容:
<form method="post" enctype="multipart/form-data">
您应该首先检查HttpContext.Request.Form.Files.Length是否大于0,或者代码是否会抛出错误。然后将字节复制到模型以保存。
另请参见查看并注意
enctype
属性。发布表单时,它仍然显示null而不是文件。关于为什么会发生这种情况,您还有其他想法吗?检查以下教程您是否也考虑过使用一个模型类。将IFormFile属性合并到一个模型中,它应该可以工作。从文档中,您还应该注意,您需要在表单上包含enctype=“multipart/form data”
。我刚刚尝试将其添加到From中,但当我尝试发布表单时,属性仍然为null。你有没有其他的想法来解释是什么原因造成的?@JoeAudette我不认为你说的是真的,ifformfile
的模型绑定应该可以正常工作。我的猜测是,多部分/表单数据
确实起到了作用。@HenkMollema你是对的,文档看起来应该可以工作,但op说这并不能解决问题。我将编辑我的答案,如果您对文件模型绑定有任何问题,您可以直接从Form.files获得它。我明白了,这很奇怪。谢谢你的澄清。
var formFile = HttpContext.Request.Form.Files[0];