Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试将blob传递给C#控制器时为Null参数_C#_Javascript_Php_Asp.net Mvc - Fatal编程技术网

尝试将blob传递给C#控制器时为Null参数

尝试将blob传递给C#控制器时为Null参数,c#,javascript,php,asp.net-mvc,C#,Javascript,Php,Asp.net Mvc,所以我指的是将blob保存到我的服务器。但是,我无法将这个PHP句子翻译成C 查看模型 public class SoundBlob { public string key { get; set; } public HttpPostedFileBase blob { get; set; } // I tried byte[] and string too } 控制器 [HttpPost] public ActionResult Su

所以我指的是将blob保存到我的服务器。但是,我无法将这个PHP句子翻译成C

查看模型

public class SoundBlob
{        
    public string key { get; set; }
    public HttpPostedFileBase blob { get; set; } // I tried byte[] and string too
}
控制器

       [HttpPost]
        public ActionResult SubmitSound(SoundBlob 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);

            blob.blob.SaveAs(blob.key);
            w.Close();
            fs.Close();           
            return new JsonResult() { Data = "Saved successfully" };
        }
SoundBlob
键和
blob
均为空!如何修复它?

试试以下方法:

客户端:

<script src="http://code.jquery.com/jquery-1.10.2.min.js" ></script>
<div class='wrapper' >
    <input type='file' class='target' id='targetinput' />
</div>
var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append("blob.blob", document.getElementById('targetinput').files[0]);
xhr.open("POST", "/Home/SubmitSound", true);
xhr.send(fd);
[HttpPost]
public ActionResult SubmitSound(SoundBlob blob)
{
        var fileName = Server.MapPath(string.Format("/Content/Sound/{0}.wav", Environment.TickCount));
        blob.blob.SaveAs(fileName);
        return new JsonResult { Data = "Saved successfully" };
}
服务器:

<script src="http://code.jquery.com/jquery-1.10.2.min.js" ></script>
<div class='wrapper' >
    <input type='file' class='target' id='targetinput' />
</div>
var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append("blob.blob", document.getElementById('targetinput').files[0]);
xhr.open("POST", "/Home/SubmitSound", true);
xhr.send(fd);
[HttpPost]
public ActionResult SubmitSound(SoundBlob blob)
{
        var fileName = Server.MapPath(string.Format("/Content/Sound/{0}.wav", Environment.TickCount));
        blob.blob.SaveAs(fileName);
        return new JsonResult { Data = "Saved successfully" };
}

完美地工作,我刚刚检查了它

不依赖于C#类型,而是使用这个脚本

            var length = Request.ContentLength;
            var bytes = new byte[length];
            Request.InputStream.Read(bytes, 0, length);

很抱歉,我没有提供足够的背景资料。基本上,我使用一个javascript库来记录一个文件,当记录完成时,一个
blob
被传递给一个函数,以将其保存到客户端机器。如果您查看的源代码,您可以看到功能
doneEncoding
,这是我试图修改的功能。我试图实现的是用户可以单击按钮,录制他们的声音,然后再次单击同一按钮,然后
blob
可以上载到数据库。恐怕您的解决方案需要用户选择一个文件。这是为我做的。干得好。