I am trying upload image from ASP.NET Core 6 with Blazor to SQL Server. if file size small then it gets uploaded, but if file size is 2 MB or more, then SQL Server insert only size 64 KB, 65535 chars, but my string size is a lot more. My actual image size 2.64 MB.
My SQL Server table structure
CREATE TABLE [dbo].[CustomerDocUnAuth]
(
[CusDocTypeId] [varchar] (14) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DocFile_64] [varchar] (max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerDocUnAuth]
ADD CONSTRAINT [PK_CustomerDocUnAuth]
PRIMARY KEY CLUSTERED ([CusDocTypeId])
WITH (FILLFACTOR=90, PAD_INDEX=ON) ON [PRIMARY]
GO
And this is my C# code
protected async Task SingleUpload(InputFileChangeEventArgs e)
{
try
{
var file = e.File;
if (!string.IsNullOrEmpty(file.Name))
{
base64String = "";
long maxFileSize = 1024L * 1024L * 1024L * 2L;
await using MemoryStream fs = new MemoryStream();
await file.OpenReadStream(maxFileSize).CopyToAsync(fs);
byte[] somBytes = GetBytes(fs);
base64String = Convert.ToBase64String(somBytes, 0, somBytes.Length);
string sql = $@"INSERT INTO CustomerDocUnAuth (CusDocTypeId, DocFile_64) VALUES('{id}', '{base64String}')";
int i = da.Insert(sql);
}
}
catch (Exception ex)
{
}
}
1条答案
按热度按时间xfb7svmp1#
There is a lot wrong with your code. I have no idea if any of this will fix your issue, but there are often issues when you directly inject data into TSQL. Always use parameters.
Furthermore:
varbinary(max)
directly.using
.SqlCommand.ExecuteNonQuery
instead.async
. Also consider using aCancellationToken
.using
.MemoryStream
either, which is also wasteful. You can stream the file directly via the parameter.