0

I working on a uploading image upload from Blazor webassembly project, I send the IFormFile file to an API that convert the image to base64 string and then I called an sql stored procedure to save the string in a SQL Server database table.

The type of the base64 string column in the table is NVARCHAR(MAX) but I noticed when the string is saved, SQL truncates the string note : I changed the data type of column to VARCHAR(MAX) or ntext but same problem. when I tried to insert from sql studio management same problem here is the code


using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                await image.File.CopyToAsync(fileStream);
            }

            string base64String;
            using (var fs1 = image.File.OpenReadStream())
            using (var ms = new MemoryStream())
            {
                fs1.CopyTo(ms);
                image.UploadFile = ms.ToArray();
            }

          
            base64String = Convert.ToBase64String(image.UploadFile);




wwhen I take the value of base64String and see if the image is correct via https://codebeautify.org/base64-to-image-converter the image appears correctly so I thing the problem in sql.

ALTER Procedure [dbo].[InsertCustomerFiles]

@CustomerID int,
@UploadFile nvarchar(MAX)


as

INSERT INTO  [dbo].[CustomoerFiles]
(      [CustomerID]
      ,[UploadFile])

      values
      (@CustomerID
      ,@UploadFile
      )

how I call stored procedure from asp.net core webapi

 using (IDbConnection CISConnection = DbConnection)
            {
                try
                {

                    DynamicParameters parameters = new DynamicParameters();
                    // Form Consent
                    parameters.Add("CustomerID", int.Parse(image.CustomerID));
                    parameters.Add("FileName", image.FileName);
                    parameters.Add("FilePath", filePath);
                    parameters.Add("FileType", int.Parse(image.FileType));
                    parameters.Add("UploadFile", base64String);
                      CISConnection.ExecuteReader("InsertCustomerFiles", parameters, commandType: CommandType.StoredProcedure);
                }
                // return SqlMapper.Query<SkinType>(con, "GetSkinType", param: parameters, commandType: CommandType.StoredProcedure);
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }
user2777546
  • 107
  • 1
  • 2
  • 8
  • After you insert it, please check the length (using both `LEN` and `DATALENGTH`). Please share that value in your question, alongside the value of `base64String.Length`. – mjwills Apr 04 '21 at 12:52
  • @mjwills it's truncated because when I select and check the result with the original string it's not the same. – user2777546 Apr 04 '21 at 12:57
  • Tell me _exactly_ how you checked it. What is the `LEN` and `DATALENGTH` value? – mjwills Apr 04 '21 at 12:57
  • I do SELECT [CustomerID] ,[UploadFile] FROM dbo.[CustomoerFiles] then I past the result of Uploadfile to https://codebeautify.org/base64-to-image-converter and see that the image is truncated. – user2777546 Apr 04 '21 at 13:00
  • I do select using SSMS – user2777546 Apr 04 '21 at 13:03

0 Answers0