Skip to content

fix: DivideByZero in FluentInputFile #3717

@jo0ls

Description

@jo0ls

🐛 Bug Report

If you set the FluentInputFile to Stream, and upload a 0 byte file then it gets a divide by zero error and never completes the upload.

💻 Repro or Code Sample

Start a new fluent web app, interactive server. Set detailed error in appsettings:

{
  "DetailedErrors": true,
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Amend the home component, adding a FluentInputFile set to stream:

@page "/"

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

<p>@completed</p>

<FluentInputFile @ref="@myFileUploader"
DragDropZoneVisible="false"
Mode="InputFileMode.Stream"
Multiple="true"
AnchorId="MyUploadButton"
MaximumFileSize="@(100 * 1024 * 1024)"
Accept="*"
OnCompleted="@OnCompleted" />

<FluentButton Id="MyUploadButton" Appearance="Appearance.Accent">
  Upload files
</FluentButton>

@if (Files.Any())
{
  <h4>File(s) uploaded:</h4>
  <ul>
    @foreach (var file in Files)
    {
      <li>
        <b>@file.Name</b> 🔹
        @($"{Decimal.Divide(file.Size, 1024):N} KB") 🔹
        @file.ContentType 🔹
        @file.LocalFile?.FullName
        @file.ErrorMessage
      </li>
    }
  </ul>
}

@code
{
  FluentInputFile? myFileUploader = default!;
  string completed = "not completed";

  FluentInputFileEventArgs[] Files = Array.Empty<FluentInputFileEventArgs>();

  void OnCompleted(IEnumerable<FluentInputFileEventArgs> files)
  {
    completed = "completed";
    Files = files.ToArray();    
  }
}

Create a new text file, it will be 0 bytes. Attempt to upload the file.

🤔 Expected Behavior

It should upload the file and completed should fire and display the file details.

😯 Current Behavior

Nothing happens.
With detailed errors, the browser console shows a divide by zero:

blazor.web.js:1 Uncaught (in promise) Error: System.DivideByZeroException: Attempted to divide by zero.
   at :7149/System.Decimal.DecCalc.VarDecDiv(DecCalc& d1, DecCalc& d2)
   at :7149/System.Decimal.Divide(Decimal d1, Decimal d2)
   at :7149/Microsoft.FluentUI.AspNetCore.Components.FluentInputFile.UpdateProgressAsync(Int64 current, Int64 size, String title) in /_/src/Core/Components/InputFile/FluentInputFile.razor.cs:line 406
   at :7149/Microsoft.FluentUI.AspNetCore.Components.FluentInputFile.OnUploadFilesHandlerAsync(InputFileChangeEventArgs e) in /_/src/Core/Components/InputFile/FluentInputFile.razor.cs:line 323
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(:7149/Task task)
    at w.endInvokeDotNetFromJS (blazor.web.js:1:3158)
    at vn._invokeClientMethod (blazor.web.js:1:65750)
    at vn._processIncomingData (blazor.web.js:1:63139)
    at connection.onreceive (blazor.web.js:1:56780)
    at i.onmessage (blazor.web.js:1:80728)

💁 Possible Solution

...

🔦 Context

It's not a big deal, but in testing people are creating empty files and reporting it.
It does upload them if set to SaveToTemporaryFolder instead of stream.

🌍 Your Environment

Win11, VS 2022, Chrome/Edge.
Net9, fluent 4.11.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew issue. Needs to be looked at

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions