SOURCE

console 命令行工具 X clear

                    
>
console
$(function(){
$("#saveButton").click(function () {

                var fordata = new FormData();
                fordata.append('file', $('#UserPhoto')[0].files[0]);
                $.ajax({
                    url: '/Home/UpImg',
                    type: 'post',
                    async: false,
                    //cache: false,
                    processData: false,
                    contentType: false,
                    //enctype: 'multipart/form-data',
                    data: fordata,
                    xhr: function () {
                        return $.ajaxSettings.xhr();
                    },
                    success: function (data) {  //图片上传完成后再上传其他信息
                        $.ajax({
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/Home/saveUser",//url
                            data: $('#editUserForm').serialize(),
                            success: function (result) {
                                $(".second.modal .description").text(result.indexOf("OK") > -1 ? "操作成功" : "操作失败");
                                $(".second.modal").modal('show');
                                $(".second.modal .button").unbind("click");
                                $(".second.modal .button").click(function () {
                                    $(".second.modal").modal('hide');
                                });
                            },
                            error: function () {
                                $(".second.modal .description").text("操作失败");
                                $(".second.modal").modal('show');
                                $(".second.modal .button").unbind("click");
                                $(".second.modal .button").click(function () {
                                    $(".second.modal").modal('hide');
                                });
                            }
                        });
                    }
                });
            });

})
<input name="UserPhoto" formenctype="multipart/form-data" id="UserPhoto" type="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" class="form-control" />
<button id="saveButton">上传</button>


<!--
    控制器部分代码
    public async Task<ActionResult> UpImg(IFormFile file)
        {

         //   HttpPostedFileBase file = (HttpPostedFileBase)Request.Form.Files["file"];
            //判断文件是否存在
            if (file != null)
            {
                //写一个文件保存的路径
                string imgpath = _appEnvironment.WebRootPath + "\\src\\userPhoto";
                //判断路径是否存在,不存在则创建
                if (!Directory.Exists(imgpath))
                {
                    Directory.CreateDirectory(imgpath);
                }
                //给文件再起个名
                string ImgName = Utils.Md5Hepler.GenerateMD5(_context.Users.Where(u => u.UserName == Request.Cookies["user"]).FirstOrDefault().UserName);// DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + file.FileName;
                //把文件保存到服务器上
                using (var stream = System.IO.File.Create(imgpath + "\\" + ImgName + ".jpg"))
                {
                    await file.CopyToAsync(stream);
                }
                _context.Users.Where(u => u.UserName == Request.Cookies["user"]).FirstOrDefault().photo = ImgName + ".jpg";
                await _context.SaveChangesAsync();

                //返回文件的位置
                return Json("{result:OK}");
            }

            return Json("{result:NG}");
        }
-->

本项目引用的自定义外部资源