public async Task <JsonResult> AjaxFileUp()
        {
            var data = new MoData {
                Msg = "上传失败"
            };

            try
            {
                var files = Request.Form.Files.Where(b => b.Name == "MyPhoto01");
                //非空限制
                if (files == null || files.Count() <= 0)
                {
                    data.Msg = "请选择上传的文件。"; return(Json(data));
                }
                //格式限制
                var allowType = new string[] { "image/jpeg", "image/png" };
                if (files.Any(b => !allowType.Contains(b.ContentType)))
                {
                    data.Msg = $"只能上传{string.Join(",", allowType)}格式的文件。";
                    return(Json(data));
                }
                //大小限制
                if (files.Sum(b => b.Length) >= 1024 * 1024 * 4)
                {
                    data.Msg = "上传文件的总大小只能在4M以下。"; return(Json(data));
                }
                //写入服务器磁盘
                foreach (var file in files)
                {
                    var fileName = file.FileName;
                    var webroot  = _hostingEnvironment.WebRootPath + "\\images\\";
                    var path     = Path.Combine(webroot, fileName);
                    using (var stream = System.IO.File.Create(path))
                    {
                        await file.CopyToAsync(stream);
                    }
                }
                data.Msg    = "上传成功";
                data.Status = 2;
            }
            catch (Exception ex)
            {
                data.Msg = ex.Message;
            }
            return(Json(data));
        }
        public JsonResult AjaxFileUp03()
        {
            var data = new MoData {
                Msg = "上传失败"
            };

            try
            {
                var files = Request.Form.Files.Where(b => b.Name == "MyPhoto03");
                //非空限制
                if (files == null || files.Count() <= 0)
                {
                    data.Msg = "请选择上传的文件。"; return(Json(data));
                }
                //格式限制
                var allowType = new string[] { "image/jpeg", "image/png" };
                if (files.Any(b => !allowType.Contains(b.ContentType)))
                {
                    data.Msg = $"只能上传{string.Join(",", allowType)}格式的文件。";
                    return(Json(data));
                }
                //大小限制
                if (files.Sum(b => b.Length) >= 1024 * 1024 * 4)
                {
                    data.Msg = "上传文件的总大小只能在4M以下。"; return(Json(data));
                }
                //初始化上传多个文件的Bar,存储到缓存中,方便获取上传进度
                var listBar = new List <MoBar>();
                files.ToList().ForEach(b =>
                {
                    listBar.Add(new MoBar
                    {
                        FileName = b.FileName,
                        Status   = 1,
                        CurrBar  = 0,
                        TotalBar = b.Length
                    });
                });
                _cache.Set <List <MoBar> >(cacheKey03, listBar);
                var    len   = files.Count();
                Task[] tasks = new Task[len];
                //写入服务器磁盘
                for (int i = 0; i < len; i++)
                {
                    var file = files.Skip(i).Take(1).SingleOrDefault();
                    tasks[i] = Task.Factory.StartNew((p) =>
                    {
                        var item = p as IFormFile;
                        //总大小
                        var totalSize = item.Length;
                        //初始化每次读取大小
                        var readSize = 1024L;
                        var bt       = new byte[totalSize > readSize ? readSize : totalSize];
                        //当前已经读取的大小
                        var currentSize = 0L;
                        var fileName    = item.FileName;
                        var webroot     = _hostingEnvironment.WebRootPath + "\\images\\";
                        var path        = Path.Combine(webroot, fileName);
                        using (var stream = System.IO.File.Create(path))
                        {
                            //进度条处理流程
                            using (var inputStream = item.OpenReadStream())
                            {
                                //读取上传文件流
                                while (inputStream.Read(bt, 0, bt.Length) > 0)
                                {
                                    //当前读取的长度
                                    currentSize += bt.Length;
                                    //写入上传流到服务器文件中
                                    stream.Write(bt, 0, bt.Length);
                                    //获取每次读取的大小
                                    readSize = currentSize + readSize <= totalSize ?
                                               readSize :
                                               totalSize - currentSize;
                                    //重新设置
                                    bt = new byte[readSize];
                                    //设置当前上传的文件进度,并重新缓存到进度缓存中
                                    var bars        = _cache.Get <List <MoBar> >(cacheKey03);
                                    var currBar     = bars.Where(b => b.FileName == fileName).SingleOrDefault();
                                    currBar.CurrBar = currentSize;
                                    currBar.Status  = currentSize >= totalSize ? 2 : 1;
                                    _cache.Set <List <MoBar> >(cacheKey03, bars);
                                    System.Threading.Thread.Sleep(200 * 1);
                                }
                            }
                        }
                    }, file);
                }
                //任务等待 ,这里必须等待,不然会丢失上传文件流
                Task.WaitAll(tasks);
                data.Msg    = "上传完成";
                data.Status = 2;
            }
            catch (Exception ex)
            {
                data.Msg = ex.Message;
            }
            return(Json(data));
        }
        public async Task <JsonResult> AjaxFileUp02()
        {
            var data = new MoData {
                Msg = "上传失败"
            };

            try
            {
                var files = Request.Form.Files.Where(b => b.Name == "MyPhoto02");
                //非空限制
                if (files == null || files.Count() <= 0)
                {
                    data.Msg = "请选择上传的文件。"; return(Json(data));
                }
                //格式限制
                var allowType = new string[] { "image/jpeg", "image/png" };
                if (files.Any(b => !allowType.Contains(b.ContentType)))
                {
                    data.Msg = $"只能上传{string.Join(",", allowType)}格式的文件。";
                    return(Json(data));
                }
                //大小限制
                if (files.Sum(b => b.Length) >= 1024 * 1024 * 4)
                {
                    data.Msg = "上传文件的总大小只能在4M以下。"; return(Json(data));
                }
                //初始化上传多个文件的Bar,存储到缓存中,方便获取上传进度
                var listBar = new List <MoBar>();
                files.ToList().ForEach(b =>
                {
                    listBar.Add(new MoBar
                    {
                        FileName = b.FileName,
                        Status   = 1,
                        CurrBar  = 0,
                        TotalBar = b.Length
                    });
                });
                _cache.Set <List <MoBar> >(cacheKey, listBar);
                //写入服务器磁盘
                foreach (var file in files)
                {
                    //总大小
                    var totalSize = file.Length;
                    //初始化每次读取大小
                    var readSize = 1024L;
                    var bt       = new byte[totalSize > readSize ? readSize : totalSize];
                    //当前已经读取的大小
                    var currentSize = 0L;
                    var fileName    = file.FileName;
                    var webroot     = _hostingEnvironment.WebRootPath + "\\images\\";
                    var path        = Path.Combine(webroot, fileName);
                    using (var stream = System.IO.File.Create(path))
                    {
                        //await file.CopyToAsync(stream);
                        //进度条处理流程
                        using (var inputStream = file.OpenReadStream())
                        {
                            //读取上传文件流
                            while (await inputStream.ReadAsync(bt, 0, bt.Length) > 0)
                            {
                                //当前读取的长度
                                currentSize += bt.Length;
                                //写入上传流到服务器文件中
                                await stream.WriteAsync(bt, 0, bt.Length);

                                //获取每次读取的大小
                                readSize = currentSize + readSize <= totalSize ? readSize :
                                           totalSize - currentSize;
                                //重新设置
                                bt = new byte[readSize];
                                //设置当前上传的文件进度,并重新缓存到进度缓存中
                                var bars    = _cache.Get <List <MoBar> >(cacheKey);
                                var currBar = bars.Where(b => b.FileName == fileName).SingleOrDefault();
                                currBar.CurrBar = currentSize;
                                currBar.Status  = currentSize >= totalSize ? 2 : 1;
                                _cache.Set <List <MoBar> >(cacheKey, bars);
                                System.Threading.Thread.Sleep(100 * 1);
                            }
                        }
                    }
                }
                data.Msg    = "上传完成";
                data.Status = 2;
            }
            catch (Exception ex)
            {
                data.Msg = ex.Message;
            }
            return(Json(data));
        }