IFormFile is null
I wrestled with this for a couple of hours.
If you have a model with only the IFormFile property - in the OnPostAsync the property is null. If I looked at the HttpContext.Request.Form.Files - the file was there. After surfing around in StackOverflow, I found a workaround. I added another property to my model
public string WhatTheHeck { get; set; }
and added this to my form
<div class="form-group"> <input type="hidden" asp-for="InputImportTestTypes.WhatTheHeck" /> </div>
After that it worked perfectly. From what I read this was supposed to be fixed in 2.0 but it is still there in 2.1.
You may want to mention it in the document.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 78f20f2e-587a-57cb-2f10-07801087f963
- Version Independent ID: 24123f8a-e64d-ca43-368b-7cdfe31aa77c
- Content: Upload files to a Razor Page in ASP.NET Core
- Content Source: aspnetcore/razor-pages/upload-files.md
- Product: aspnet-core
- GitHub Login: @guardrex
- Microsoft Alias: riande
Interesting (and unexpected). I didn't hit this when I set things up with a bound class that contains the IFormFile.
I'm interested in including a note in the topic, but I will need to find some time to see what's what ... I'd like to repro what you found here so that I can see what's going on. I'm a bit 🏃 slammed 🏃😅 at the moment, but I will take a look at this as soon as I'm free (couple of weeks ... or a month lol) ... ASAP.
Thanks @RetrotecEd for describing the scenario.
You made my day :) after a few hours spent to figure out what is wrong, I just read your comment. Workaround works for me as well.
I'm going to close this because I can't repro it here. It's possible that it was a known issue that was fixed by engineering since this issue was opened.
Here's what I did in the sample app ...
I took the sample app to 2.2 ... TFM + MVC compat version.
I changed the FileUpload class to only have the IFormFile ...
public class FileUpload
{
[Required]
[Display(Name="Private Schedule")]
public IFormFile UploadPrivateSchedule { get; set; }
}
I updated the pages (Index and Delete) accordingly to drop the public schedule and title. For the Schedule class property Title, I assigned a string, since I removed the Title prop from the FileUpload class ...
var schedule = new Schedule()
{
PrivateSchedule = privateScheduleData,
PrivateScheduleSize = FileUpload.UploadPrivateSchedule.Length,
Title = "UPLOADED SCHEDULE",
UploadDT = DateTime.UtcNow
};
... and the result was all good :+1: ...
If you can provide a sample repro (in the form of a GH sample app) to engineering, open an issue in the aspnet/AspNetCore issues for the engineers to take a look.