尝试将blob传递给C#控制器时为Null参数
所以我指的是将blob保存到我的服务器。但是,我无法将这个PHP句子翻译成C 查看模型尝试将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
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
可以上载到数据库。恐怕您的解决方案需要用户选择一个文件。这是为我做的。干得好。