0

I have found several answers (here for example) but they do not seem to solve my problem.

                var result = {
                    command: 'exportWAV',
                    type: type
                };
                $.ajax({
                    url: 'SubmitSound',
                    type: 'Post',
                    data: JSON.stringify(result),
                    contentType: 'application/json; charset=utf-8',
                    success: function (msg) {
                        alert(msg);
                    }
                });

Back end code

        [HttpPost]
        public ActionResult SubmitSound(string blob)
        {
            // Create the new, empty data file.
            string fileName = AppDomain.CurrentDomain.BaseDirectory + "/Content/Sound/" + Environment.TickCount + ".wav";
            FileStream fs = new FileStream(fileName, FileMode.CreateNew);
            BinaryWriter w = new BinaryWriter(fs);

            w.Write(blob);
            w.Close();
            fs.Close();           
            return new JsonResult() { Data = "Saved successfully" };
        }

result is not null because this.postMessage = result; send a file back to client side for downloading. w.Write(blob) keeps complaining that blob cannot be null.

How can I make it work? Thank you and best regards

Community
  • 1
  • 1
Minh Triet
  • 1,190
  • 1
  • 15
  • 35

1 Answers1

0

Do this:

data: JSON.stringify({ blob: result}),

You may want to change your string param in your controller action, for an object with the same structure of your JSON... that means the same attributes names.

The object should be something like this:

public class MyBlob{
  public string command {get; set;}
  public string type {get; set;}
}

So, your action should be:

 public ActionResult SubmitSound(MyBlob blob){
    //Here your action logic
 }
Romias
  • 13,783
  • 7
  • 56
  • 85