locked
How to upload files to db with Blazor and dapper? RRS feed

  • Question

  • User-180482348 posted

    in EF Core it was  "foreach( var file in Request.Form.Files){db.Para.Add("foto",fotobrowser.foto,DbType.binary);

    db.Para.Add( "imageTitle",fotobrowser.imageTitle,DbType.String);}

    but how to upload byte[] files and their names to sql server db?

    public Task <int> Create(Main main, Transport transport,Fotobrowser fotobrowser)
    {
      var dbPara=new DynamicParameters();
    dbPara.Add("calendar",main.calendar,DbType.DateTime);
    dbPara.Add("tr_name",transport.tr_name,DbType.String);
    //dbPara.Add("foto", fotobrowser.foto, DbType.Binary); doesn't work
    var bookId=Task.FromResult(_dapperService.Insert<int>("[dbo].[spAddMain]",dbPara,commandType:CommandType.StoredProcedure));
    return bookId;
    }
    @page "/addrecord"
    @inject IMainService mainService
    @inject ITransportService transportService
    @inject IFotobrowserService fotobrowserService
    @inject Microsoft.AspNetCore.Components.NavigationManager navigationManager
    <form asp-action="Create" enctype="multipart/form-data" method="post">
    <div class="row">
    <div class="col-md-8"
    <div class=form-group">
    <label for="calendar" class="control-label"<Date</label>
    <input for="calendar" class="form-control"
    @bind="@main.calendar"/>
    </div>
    
    <div class="form-group">
    <label for="tr_name" class="control-label">Car's Number</label>
    <input for="tr_name" class="form-control"
    @bind="@transport.tr_name"/>
    </div>
    <div class="form-group">
    <label for="fotobrowser.foto" class="control-label"></label>
    <input type="file" id="file1" name="file1" multiple />
    </div>
    ...
    <button type="button" class="btn btn-primary"
    @onclick="()=>CreateRecord()">Save</button>

    @code{
    Main main=new Main();
    Transport transport=new Transport();
    Fotobrowser fotobrowser=new Fotobrowser();

    protected async Task CreateRecord()
    {
    await mainService.Create(main,transport,fotobrowser);
    navigationManager.NavigateTo("/mainlist);
    }
    Monday, September 28, 2020 10:41 AM

Answers

All replies

  • User-474980206 posted

    Server blazor does not have support for file upload. For client blazor you use JavaScript (google for examples). The issuer with server, is that the file must be uploaded via signal/r and there are some restrictions.  See

    https://blog.stevensanderson.com/2019/09/13/blazor-inputfile/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 28, 2020 2:57 PM
  • User-180482348 posted

    bruce (sqlwork.com)

    Server blazor does not have support for file upload. For client blazor you use JavaScript (google for examples). The issuer with server, is that the file must be uploaded via signal/r and there are some restrictions.  See

    https://blog.stevensanderson.com/2019/09/13/blazor-inputfile/

    I have some problems with BlazorInputFile: when I press "Choose File" and select some file it appears on 1-2 sec in app and then disappears. How to fix this?

    Wednesday, September 30, 2020 9:37 AM