C# MVC ajax上传 文件
用普通的ajax提交表单的时候,不能把文件流传到后端去,所以要用到jquery.form.js
jquery.form.js到官网下载或者从这里下载: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://www.cnblogs.com/wei325/p/5597015.html
下面这个
FormData上传文件,我当时用了 好像是不支持IE10以下
var formData = new FormData();
formData.append("filefloder", document.getElementById("filefloder").files[0])
$.ajax({
url: "@Url.Action("Upload", "UpdateManage")",
type: "post",
data: formData,
contentType: false,
processData: false,
success: function () {
alert("上传成功")
location = location
},
error: function () {
alert("上传失败")
}
})
无穷的伟大,也是从“0”开始的