當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

asp.net MVC ajax上傳文件
2021-10-22 10:03:31

普通上傳

view:



<body>
<form id="form1" method="post" action="@Url.Action("UploadFile","Mydemo")" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="提交" />
</form>
</body>


Controller:



[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
return Content("");
}


Ajax上傳

? 用普通的ajax提交表單的時候,不能把文件流傳到后端去,所以要用到j(luò)query.form.js

? jquery.form.js到官網(wǎng)下載或者從這里下載:??http://pan.baidu.com/s/1c2JS60C??

view:



<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.form.js"></script>
</head>
<body>
<form id="form1">
<input type="file" name="file"/>
<input type="button" value="提交" onclick="sub()" />
</form>
</body>
</html>
<script type="text/javascript">
function sub()
{
$("#form1").ajaxSubmit({
url: "@Url.Action("UploadFile", "Mydemo")",
type: "post",
success: function (data) {
//......
},
error: function (aa) {
alert(aa);
}
});
}
</script>


Controller:



[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{

return Content("");
}


?

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >