本文介绍了如何使用ASP.NET MVC3将图像文件从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Helo朋友,
我如何使用asp.net mvc3将Ajax或jquery的图像文件从视图传输到控制器.
我的查看文件看起来像
Helo freinds,
How can i transfer image file using ajax or jquery from view to controller using asp.net mvc3.
My view file looks like
<script type="text/javascript">
$(document).ready(function () {
$("#photos").kendoUpload();
$("#save").click(function (event) {
alert("started");
url = 'Home/Details'
var data = $('#photos').load(url)
$.ajax({
type: "POST",
url: '/Home/Details',
//data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (str) {
alert("hai");
alert(str.st);
}
});
</script>
<body>
<input name="photos" id="photos" type="file" />
<input type="button" value="save" id="save" />
</body>
我的控制器文件看起来像
My controller file looks like
public ActionResult Details(HttpPostedFileBase data)
{
try
{
data = Request.Files[1];
byte[] imageSize = new byte[data.ContentLength];
data.InputStream.Read(imageSize, 0, (int)data.ContentLength);
Image g = new Image();
g.Img = imageSize;
dbContext.Add(g);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
catch
{
}
var str = new { st = "saved" };
return Json(str, JsonRequestBehavior.AllowGet);
}
当我上传文件并单击保存按钮时,我的控制器获取了空文件.html5中是否有任何方法来传输文件.如果是,请给我任何建议.
等待答案
When i uploads the file and click it on save button my controller takes the null file.Is there any methos in html5 to transfer the file .If so please provide me any suggestions.
Waiting for your answers
推荐答案
这篇关于如何使用ASP.NET MVC3将图像文件从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!