I'm trying to do a simple file upload in asp.net core 2 razor pages. I have the code below. Please realize that it is imcomplete. When i run in my VS2017, I check my FileUpload object, and it is unfortunately null. I would hope that it is something besides null and I could create a stream to get some data out of it. However, with the object being null, I would suspect that I do not have something tied together correctly. Any thoughts are appreciated. Thanks.
Code behind cs:
public class PicturesModel : PageModel
{
public PicturesModel()
{
}
[Required]
[Display(Name = "Picture")]
[BindProperty]
public IFormFile FileUpload { get; set; }
public async Task OnGetAsync()
{
}
public async Task<IActionResult> OnPostAsync()
{
//FileUpload is null
return RedirectToPage("/Account/Pictures");
}
}
front end cshtml file;
<form method="post" enctype="multipart/form-data">
<label asp-for="FileUpload"></label>
<input type="file" asp-for="FileUpload" id="file" name="file" />
<input type="submit" value="Upload" />
</form>
Your property for the file input is named
FileUploadbut you have overridden thenameattribute generated by the TagHelper and renamed it tofilewhich does not match the property name.Change the view code to
so that it generates the correct
nameattribute (which isname="FileUpload"). Note also the removal ofid="file"which means that you<label>would not have set focus to the associated control when clicked).