.NET Core等效于Uint8ClampedArray

时间:2018-07-12 21:40:41

标签: javascript c# asp.net-core uint8array

我正在使用jpeg_camera库从笔记本电脑上的Web应用程序获取快照,并通过提取调用将其发送到控制器。

snapshot.get_image_data返回一个具有3个属性的对象(数据:Uint8ClampedArray,宽度,高度)。

当我执行提取调用sendPic(data)时,总是收到400错误,因为ModelState无效。
这意味着Byte[]对JS中的Uint8ClampedArray不利。

该对象的等效物是什么?

我还发现了一种返回base64的方法,可以将其在控制器内转换为Byte[],但我想避免这种解决方案。

JS代码:

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

API控制器:

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

和CLASS“ Selfie”:

public class Selfie
{
    public Byte[] Pic { get; set; }
    public int IdActivity { get; set; }
    public DateTime Instant { get; set; }
}

1 个答案:

答案 0 :(得分:0)

使用stringify时,它在JSON中变成{"0":255, "1":0, "2":0, "3":255, ...}

如果您将操作参数定义为JObject selfie,则可以使用var pic = selfie["Pic"].ToObject<Dictionary<int, byte>>()获取字节。

或者,您可以将数据作为表单值发布,并使用浏览器中的以下代码将操作定义为SavePic(Dictionary<int, byte> Pic, int IdActivity, DateTime Instant)进行测试:

var ctx = document.createElement('canvas').getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(5, 5, 5, 5);

$.post('SavePic', {
    Pic: ctx.getImageData(5, 5, 5, 5).data,
    IdActivity: 1,
    Instant: new Date().toISOString()
});

但是,在发布数据之前将Uint8ClampedArray转换为常规数组会更容易。