zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    USACO 4.1 Fence Rails
    POJ 1742
    LA 2031
    uva 10564
    poj 3686
    LA 3350
    asp.net MVC 3多语言方案--再次写, 配源码
    使用Log4net记录日志
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    为什么要使用反射机制
  • 原文地址:https://www.cnblogs.com/sjqq/p/9147580.html
Copyright © 2011-2022 走看看