I am making a website with profiles of users and there they can upload their avatar. And I need to get a photo from user and store this in users database. First got an image from user and send information to server:
saveButton.onclick = (() => {
const file = photoInput.files[0];
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = (async () => {
const bytes = reader.result;
const description = descriptionInput.value;
const data = JSON.stringify({
photo: bytes,
description
});
await fetch("[username]/changedata", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: data
});
window.location.reload();
});
});
Then tried to store this image in users database:
app.MapPut("{username}/changedata", async (string username, HttpContext context) =>
{
var data = await context.Request.ReadFromJsonAsync<UserDataChange>();
using var con = new SqlConnection(connection);
await con.OpenAsync();
var command = new SqlCommand();
command.Connection = con;
command.CommandText = "UPDATE [users] " +
"SET description=@description, picture=@picture " +
"WHERE username=@username";
command.Parameters.AddWithValue("username", username);
command.Parameters.AddWithValue("description", data.Description);
command.Parameters.AddWithValue("picture", data.Photo);
await command.ExecuteNonQueryAsync();
await con.CloseAsync();
});
UserDataChange class:
public class UserDataChange
{
public byte[] Photo { get; set; }
public string Description { get; set; }
}
But byte[] is invalid type for this situation.