AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

IFormFile is null

Open RetrotecEd opened this issue 8 years ago • 3 comments

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.

RetrotecEd avatar Oct 18 '18 19:10 RetrotecEd

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.

guardrex avatar Oct 18 '18 19:10 guardrex

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.

zsoltbartha avatar Nov 20 '18 15:11 zsoltbartha

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: ...

capture

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.

guardrex avatar Dec 21 '18 00:12 guardrex