Gaurav_0093 Gaurav_0093 - 13 days ago 5
Javascript Question

Not receiving byte array data on server side by using ajax

Hello Everyone I am using angularjs to send byte array on server side but I am not getting data on server side.
Here is my code.

$scope.Upload = function () {
var bytesToSend = [253, 0, 128, 1]
var send = new Uint8Array(bytesToSend)
var Upload = $.ajax({
type: 'POST',
url: "/UploadFile",
dataType: JSON,
contentType: false,
data: send,
success: function (send) {
toastr.success("Upload Successfully");
}
});
Upload.error(function () { console.log(bytesToSend+'gaurav') });

}


And here is my server side code

[HttpPost]
[Route("UploadFile")]
public bool UploadedFile(byte[] send)
{

return false;
//return await CenterGateWay.UpldFile(up);

}
}


I am not getting data in byte[] send it is showing null value.
please anyone help where I am wrong And I am getting data in console
here [253, 0, 128, 1].
Using firefox browser right now.

Answer

Please use following code to resolve your issue.

 $scope.Upload = function () {
    var bytesToSend = [253, 0, 128, 1];
    var send = new Uint8Array(bytesToSend)
    var Upload = $.ajax({
        type: 'POST',
        url: "/Home/UploadFile",
        dataType: "JSON",
        data: { send: btoa(String.fromCharCode.apply(null, send)) },
        success: function (send) {
            toastr.success("Upload Successfully");
        }
    });

    Upload.error(function () { console.log(bytesToSend + 'gaurav') });
}

In Controller

 [HttpPost]
 [Route("UploadFile")]
 public  bool UploadedFile(string send)
 {
     return false;
     //return await CenterGateWay.UpldFile(up);
 }

Even I got help from this link Please refer it:-