question

Decompressor-1652 avatar image
0 Votes"
Decompressor-1652 asked FeiXue-MSFT answered

How to save multiple files to database with Blazor server?

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);
     }
    
 }


dotnet-aspnet-core-blazor
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Decompressor-1652 , is it necessary to using PROCEDURE you provided or we can choose to use entity framework?

0 Votes 0 ·

IBetter with procedure, but if you know only entity framework I need that answer too

0 Votes 0 ·

1 Answer

FeiXue-MSFT avatar image
0 Votes"
FeiXue-MSFT answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.