Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputFile in Blazor SSR #51980

Closed
1 task done
Markz878 opened this issue Nov 10, 2023 · 3 comments
Closed
1 task done

InputFile in Blazor SSR #51980

Markz878 opened this issue Nov 10, 2023 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one Pillar: Complete Blazor Web

Comments

@Markz878
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When submitting a form file in Blazor SSR (no interactivity), it seems that it requires the Enchance attribute on the EditForm.
Why is that, and should this be documented somewhere?
And is it possible to use InputFile component for this, only regular seems to work?

Expected Behavior

I would expect that the EditForm with file submit works without the Enchance attribute,
and that I could use InputFile component in a Blazor SSR page.

I would also expect this documentation to be updated for these scenarios:
https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-8.0

Steps To Reproduce

Create a new Blazor 8 SSR project (no interactivity) and put this code on Home page:

@page "/"

<PageTitle>Home</PageTitle>

<EditForm method="post" Model="NewCustomer" OnValidSubmit="SubmitForm" FormName="customer">
    <input type="file" name="NewCustomer.File" />
    <button>Submit</button>
</EditForm>

@code {
    [SupplyParameterFromForm]
    public Customer? NewCustomer { get; set; }

    protected override void OnInitialized()
    {
        NewCustomer ??= new();
    }

    public async Task SubmitForm()
    {
        ArgumentNullException.ThrowIfNull(NewCustomer?.File);
        using FileStream fileStream = File.OpenWrite(NewCustomer.File.Name);
        using Stream submittedFileStream = NewCustomer.File.OpenReadStream();
        await submittedFileStream.CopyToAsync(fileStream);
    }

    public class Customer
    {
        public IFormFile? File { get; set; }
    }
}

The submit fails saying that the 'NewCustomer?.File' is null.
Adding the Enchance attribute makes this work.

Exceptions (if any)

No response

.NET Version

8.0.100-rc.2.23502.2

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Nov 10, 2023
@javiercn
Copy link
Member

javiercn commented Nov 10, 2023

@Markz878 thanks for contacting us.

Unfortunately, the InputFile is a bit special in the way it works, so for SSR purposes is best to use the input element directly and provide a name that matches the expected path in your model.

You need to set the enctype on the form for the files to be sent to the server.

@javiercn javiercn added Pillar: Complete Blazor Web enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Nov 10, 2023
@Markz878
Copy link
Author

@javiercn Yeah, if I replace the Enhance parameter with enctype="multipart/form-data" it works.
And then it also works without javascript.
Maybe this could be documented somewhere?
Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one Pillar: Complete Blazor Web
Projects
None yet
Development

No branches or pull requests

3 participants