I'm using jpeg_camera library to get a snapshot from my webapp on my laptop and, by a fetch call, send it to my controller.
snapshot.get_image_data
returns an object with 3 properties (data: Uint8ClampedArray, width, heigh).
When I do the fetch call sendPic(data)
I always get a 400 Error, because the ModelState
is not valid.
That means Byte[]
is not good for Uint8ClampedArray
from JS.
What is the equivalent for that object?
I've found also a method that return a base64 and I can convert it inside the controller into a Byte[]
but I'd like to avoid this solution.
the JS code:
function savePic() {
var test1 = snapshot.get_image_data((done) => {
var data = {
"Pic": done.data,
"IdActivity": idActivity,
"Instant": new Date().toISOString()
};
sendPic(data);
});
}
function sendPic(data) {
fetch(uriPicsEndPoint, {
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=utf-8"
},
credentials: 'include',
method: 'POST'
});
}
The API Controller:
[Authorize]
[HttpPost]
public async Task<IActionResult> SavePic([FromBody] Selfie selfie)
{
if (ModelState.IsValid)
{
try
{
var storageAccount = CloudStorageAccount.Parse(_configuration["ConnectionStrings:Storage"]);
var blobClient = storageAccount.CreateCloudBlobClient();
var camerasContainer = blobClient.GetContainerReference("selfies");
await camerasContainer.CreateIfNotExistsAsync();
var id = Guid.NewGuid();
var fileExtension = ".jpeg";
var blobName = $"{selfie.IdActivity}/{id}{fileExtension}";
var blobRef = camerasContainer.GetBlockBlobReference(blobName);
await blobRef.UploadFromByteArrayAsync(selfie.Pic, 0, selfie.Pic.Length);
string sas = blobRef.GetSharedAccessSignature(
new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read
});
var blobUri = $"{blobRef.Uri.AbsoluteUri}{sas}";
var notification = new UpdateSelfieRequest()
{
UriPic = blobUri,
IdActivity = selfie.IdActivity,
Instant = selfie.Instant
};
string serviceBusConnectionString = _configuration["ConnectionStrings:ServiceBus"];
string queueName = _configuration["ServiceBusQueueName"];
IQueueClient queueClient = new QueueClient(serviceBusConnectionString, queueName);
var messageBody = JsonConvert.SerializeObject(notification);
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
await queueClient.SendAsync(message);
await queueClient.CloseAsync();
return Ok();
}
catch
{
return StatusCode(500);
}
}
else
{
return BadRequest();
}
}
And the CLASS "Selfie":
public class Selfie
{
public Byte[] Pic { get; set; }
public int IdActivity { get; set; }
public DateTime Instant { get; set; }
}