I have a flash recorder which record user input and give me a file Byte-Array in Java-script
now i want to upload that byte-array to the server (MVC3), i wonder how can i do this ?
I have a flash recorder which record user input and give me a file Byte-Array in Java-script
now i want to upload that byte-array to the server (MVC3), i wonder how can i do this ?
You could send the byte array as raw data to the server using an AJAX request. We could implement a custom model binder which will read the raw request body and map it to a byte[]:
public class ByteArrayModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
var buffer = new byte[request.InputStream.Length];
request.InputStream.Read(buffer, 0, buffer.Length);
return buffer;
}
}
then we could have a controller action that will receive this request:
[HttpPost]
public ActionResult Upload([ModelBinder(typeof(ByteArrayModelBinder))] byte[] buffer)
{
// TODO: do something with the uploaded data
return Json(true);
}
and finally on the client send the AJAX request:
// create sample data from the A,B,C bytes:
var data = String.fromCharCode(65, 66, 67);
$.ajax({
url: '@Url.Action("upload")',
type: 'POST',
contentType: 'application/octet-stream',
processData: false,
data: data,
success: function (result) {
alert(result);
}
});
byte[] as parameter. Then in the custom model binder that I showed you could read the body of the request stream. I have tested it and it worked fine. Maybe the data you have in your script is not on the same form as I showed? Notice that the fromCharCode method doesn't take an array. It takes multiple arguments. So if you have an array [65, 66, 67], it won't work...fromCharCode for each element of your byte array and concatenate it into the final data that you will send to the server.[ModelBinder(typeof(ByteArrayModelBinder))] in front of the byte[] action argument as shown in my answer? I assume you haven't, which would explain why the custom model binder is not called.Have you tried simply posting your byte array to a controller method? If your using jquery it would just be a case of using $.ajax or $.post and sending the request to the correct location according to how you have your routing arranged.