.net mvc异步上传文件

第一种方式:需要引入jquery.form.min.js  

jquery.form.min.js下载地址

链接:https://pan.baidu.com/s/1HQbd-1cbwmnPdOJL0oj8zA 
提取码:j0hx 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>

    <script src="~/Scripts/UpLoadFile/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/UpLoadFile/jquery.form.min.js"></script>
</head>
<body>
    <div>
        <h3>mvc异步上传文件</h3>
        @using (Ajax.BeginForm("FileloadAction", "UpLoad", new AjaxOptions
        {
            Confirm = "确认上传吗?",
            HttpMethod = "post",
            OnSuccess = "func",
        }, new { enctype = "multipart/form-data", id = "fileForm" }))
        {
            <input type="file" name="Filedata" value=""  accept=".xls,.docx,.doc,.txt,.pdf" />
            @*<button type="submit">异步上传</button>*@
            <button type="button">异步上传</button>
        }
    </div>
</body>
</html>
<script>
    //当按钮被点击时执行异步提交
    $("button[type=button]").click(() => {
        //异步提交
        $("#fileForm").ajaxSubmit({
            url: "@Url.Action("FileloadAction", "UpLoad")",
            type: "post",               
            success: data => {
                alert(data);
            },
            error: () => {
                alert("出错了");
            }
        });
    });


   
</script>
Indx视图代码

控制器代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HomeWorkSystem.Controllers
{
    public class UpLoadController : Controller
    {
        
        public ActionResult Index()
        {
          
            return View();
        }
              
        public ActionResult FileloadAction(IEnumerable<HttpPostedFileBase> Filedata)
        {
            if (Filedata == null || Filedata.Count() == 0 || Filedata.ToList()[0] == null)
            {
                ViewBag.ErrorMessage = "Please select a file!!";
                return Content("请选择文件~~~");
            }
            string filePath = string.Empty;
            string rootFilePath = string.Empty;
            string fileName = string.Empty;
            //多文件上传
            foreach (HttpPostedFileBase file in Filedata)
            {
                string jsonUrl = "UpLoadHomeWork/";
                string Url_ClassName = "信息16-1/";
                string Url_LessName = "安卓移动";
                rootFilePath = AppDomain.CurrentDomain.BaseDirectory + jsonUrl + Url_ClassName + Url_LessName;
                //检查目录是否存在:不存在则创建
                if (!Directory.Exists(rootFilePath))
                {
                    Directory.CreateDirectory(rootFilePath);
                }
                //对录入上传文件的名字
                string extName = Path.GetExtension(file.FileName);
                string C_Name = "信息16-1";
                string T_Title = "JavaWeb";
                fileName = C_Name + T_Title + extName;
                //将文件上传到rootFilePath路径下
                file.SaveAs(Path.Combine(rootFilePath, fileName));
            }
            return Content(fileName+"上传成功");
        }
       

    }
}
UpLoadController

 

猜你喜欢

转载自www.cnblogs.com/fzqm-lwz/p/10646269.html