简体   繁体   中英

Binding file uploader to a model in Blazor

Binding file uploader in Blazor not working with @bind attribute in razor component

i've build the model when the form is Submit using two way binding parameter and it works for standard componenent except file uploader,

i don't know what is the paramter type for binding file uploader

here is my Submit function

 protected async Task OnSubmit()
        {
...
            error = null;
            try
            {
          ...
//nikImagewas my intention to 'hold' the file image. its type is IFormFile
                customer.NIKImage = nikImage;
                var response = await _state.PostAsync(WebsVariables.Urls.CreateCustomerDraftObject, customer);
                if (response.IsSuccessStatusCode)
                {
                  ...
                }
                else
                {
                    var msg = await response.Content.ReadAsStringAsync();
                    _toastService.ShowError(msg);
                }
                //clear();

                this.StateHasChanged();
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
        }

when i try it, the nikImage is null i expected i can manipulate the file before POST it to my web API

Your code is vague and cannot really be deciphered. However, I think that you cannot use IFromFile on client-side Blazor. You may use IFormFile in server-side Blazor.

If you want to get that data from an <input type=file> , you'd have to get the binary data via something like Getting byte array through input type = file using JS interop

Samples

https://github.com/aspnet/AspNetCore/issues/10552 https://remibou.github.io/Upload-file-with-Blazor/

Hope this helps...

thx for the info

i already find that link and it seems i do it all wrong since i can't find any newest working sample about Uploading hence i'm using syncfusion uploader file but it needs to be tweaked a bit since the default signalR message size is 32KB, so it can upload larger file (3 days looking for these)

services.AddSignalR(e =>
{
    e.MaximumReceiveMessageSize = 5000000;
});

the answer from these https://github.com/aspnet/AspNetCore/issues/11643

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM