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