1 year ago
#76298
Stauron
How to insert a large image into an SQL database
If I insert a small image, for example 1-2 kb, then it is successful, if I select an image of 70 kb or any other large one, then I always have an error. I have tried both the stored procedure and directly pass an array of bytes - error.
Exception System.Data.SqlClient.SQLException: "A transport layer error occurred while receiving results from the server. (provider: TCP provider, error: 0 - The semaphore timeout is exceeded.)"
Everything is fine with SQL Server itself, images are loaded successfully through WinForms, there are no problems.
Button:
<RadzenUpload Accept="image/*" ChooseText="Select..." Url=@($"api/upload/single/{clientid}/1") Progress="@((args) => OnProgress(args, "Loading ..."))" />
Controller and void:
[HttpPost("single/{id}/{photoid}")]
public async Task<IActionResult> Single(IFormFile file, string id,string photoid)
{
try
{
IVZBlazor.Iservice.IPhotoService serv;
string fileName = Path.GetFileName(file.FileName);
string rootpath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot");
string path = rootpath + "\\Uploads";
using var fileStream = file.OpenReadStream();
long length = file.Length;
byte[] bytes = new byte[length];
fileStream.Read(bytes, 0, (int)file.Length);
using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
file.CopyTo(stream);
}
await Save(bytes, id);
return StatusCode(200);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
private DynamicParameters SetParameter(byte[] oPhoto)
{
DynamicParameters parameters = new DynamicParameters();
parameters.Add("@photo", oPhoto);
return parameters;
}
public async Task<int> Save(byte[] photo, string clientid)
{
using (var connection = new SqlConnection(Startup.ConnectSQL))
{
await connection.OpenAsync();
var sqlStatement = @"UPDATE [dbo].[photo] SET [image1] =@photo WHERE [personid] ='" + clientid + "'";
int res = await connection.ExecuteAsync(sqlStatement, SetParameter(photo));
return res;
}
}
asp.net-core
razor
blazor
0 Answers
Your Answer