How to save multiple files to database with Blazor server?

Decompressor 81 Reputation points
2021-08-26T06:58:23.023+00:00

I want to save to database table fotoblock multiple files (photos) at once. But this code allows to save different photo names, but the same photo file multiple times (first photo that was selected). How to save all photos, which were selected by user?

CREATE TABLE [dbo].[fotoblock] (
[Id]       INT             IDENTITY (1, 1) NOT NULL,
[MainID]   INT             NULL,
[calendar] DATETIME        NULL,
[modified] DATETIME        NULL,
[img_name] NVARCHAR (50)   NULL,
[photo]    VARBINARY (MAX) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));

CREATE TYPE [dbo].[Dron] AS TABLE (
[img_name] NVARCHAR (50)   NULL,
[photo]    VARBINARY (MAX) NULL);

    CREATE PROCEDURE [dbo].[newrow] @Way Dron readonly  
as begin set nocount on
insert fotoblock(calendar,img_name,photo) select
getdate(),img_name,photo from @Way
end

public class DataAccessService
{
  public async Task<Fotoblock> Gone(IFileListEntry[] samples,byte[] filebytes)
    {
        Fotoblock one = new Fotoblock();
        using(SqlConnection con=new SqlConnection(Global.ConnectionString))
        {
            con.Open();
            using(SqlCommand cmd=con.CreateCommand())
            {
                cmd.CommandText = "dbo.newrow";

                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter par;

                par = cmd.Parameters.AddWithValue("@Way", TransformSamples(samples, filebytes));
                par.SqlDbType = SqlDbType.Structured;
                par.TypeName = "dbo.Dron";

                cmd.ExecuteNonQuery();
            }

        }

        return await Task.FromResult(one);
    }

    private static IEnumerable<SqlDataRecord> TransformSamples(IFileListEntry[] samples,byte[] filebytes)
    {
        var schema = new[]
        {

            new SqlMetaData("img_name",SqlDbType.NVarChar,50),
            new SqlMetaData("photo",SqlDbType.VarBinary,SqlMetaData.Max)

        };

        var row = new SqlDataRecord(schema);


        foreach(var i in samples)
        {
            row.SetValues(i.Name,filebytes);

            yield return row;

        }



    }
}

public class Fotoblock
{
    public int Id { get; set; }
    public int MainID { get; set; }
    public DateTime calendar { get; set; }
    public byte[] photo { get; set; }

    public string img_name { get; set; }
}


 @page "/down"

@using Data
@using System.IO
@inject DataAccessService da 

here inputfile and button save (I can not insert this snippet normally, I receive message "Access denied")

@code {
    IFileListEntry[] file;
    byte[] filebytes = null;

    void HandleFileSelected(IFileListEntry[] files)
    {
        file = files;
    }

    protected async Task SaveAll()
    {
        foreach(var m in file)
        {
            using(var ms=new MemoryStream())
            {
                await m.Data.CopyToAsync(ms);

                filebytes = ms.ToArray();
            }
        }
        await da.Gone(file, filebytes);
    }

}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,579 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Fei Xue - MSFT 1,111 Reputation points
    2021-09-01T02:08:13.113+00:00

    I am not able to find the official document about IFileListEntry. It seems that you are using third-party component or library for uploading files. To upload the multiples files for Blazor Server, you can refer to the official code sample mentioned below:

    ASP.NET Core Blazor file uploads

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.