Ajax在ASP.NET MVC中上传

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace ajaxUpload.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public JsonResult Upload()
{
var uploadedFile = Request.Files[0] as HttpPostedFileBase;
return Json(uploadedFile.FileName);
} public ActionResult Index()
{
return View();
} }
}

Index.cshtml@   

ViewBag.Title = "Home Page"}

<div class="jumbotron">
<h1>Ajax在ASP.NET MVC中上传</h1>
</div>
<div class="row">
<div id="main">
<h1>上传您的图片</h1>
<form id="uploadfrm" method="post" enctype="multipart/form-data" action="/home/upload">
<input type="file" name="images" id="images" />
</form>
<div id="response"></div>
<ul id="image-list"></ul>
</div>
<button onclick="Upload();">上传</button>
</div> <script>
function Upload() {
var form = document.getElementById('uploadfrm');
var formData = new FormData(form); // 这里如何把form的数据上传
$.ajax({
url: "/home/upload",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
</script>
上一篇:类似于微信通讯录的界面Demo


下一篇:BZOJ1229 & 洛谷2917:[USACO2008 NOV]toy 玩具 & 洛谷4480:[BJWC2018]餐巾计划问题——题解