2

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 ?

1
  • 1
    Have you tried anything? Commented Jan 15, 2012 at 8:31

2 Answers 2

2

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);
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

That didnt work with me , first you provided Data Twice so i deleted the first one , now everytime it post to the server it posts the buffer as null!
@Stacker, oops, my mistake. Well, assuming you have a byte array as shown in my example, you should be able to post it to a controller action that will take a 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...
... in this case you will have to call the fromCharCode for each element of your byte array and concatenate it into the final data that you will send to the server.
i didnt change your code at all i use String.fromCharCode(65, 66, 67); just as you provided, the funny thing is i put a breakpoint inside BindModel and it never hit it !
@Stacker, did you put [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.
|
1

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.

3 Comments

how do i do that ?can you provide me of a sample code please ? as im not sure how im gonna pot a "ByteArray" to a controller
The method of posting i was reffering to would be the same as in the comments above, something along the ines of:
'// 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); } });'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.