0

我找到了几个答案(例如这里),但它们似乎没有解决我的问题。

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

后端代码

        [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不为空,因为this.postMessage = result;将文件发送回客户端进行下载。w.Write(blob)一直抱怨blob不可能null

我怎样才能让它工作?感谢你并致以真诚的问候

4

1 回答 1

0

做这个:

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

您可能希望更改string控制器操作中的参数,以获取与 JSON 结构相同的对象......这意味着相同的属性名称。

对象应该是这样的:

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

所以,你的行动应该是:

 public ActionResult SubmitSound(MyBlob blob){
    //Here your action logic
 }
于 2013-09-20T03:31:51.430 回答