.net core mvc结合uploadify控件进行图片上传

后端代码:

/// <summary>
        /// 图片上传,上传路径: "/Uploads/Images/" + folder + "/" + saveName
        /// </summary>
        /// <param name="fileData"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadImage([FromServices]IWebHostEnvironment environment,string folder)
        {
            try
            {
                var files = Request.Form.Files;
                //string contentRootPath = environment.ContentRootPath;//项目所在路径
                string webRootPath = environment.WebRootPath;//wwwroot文件夹所在路径 
                string filePath = webRootPath + "/Uploads/Images/" + folder + "/";
                string fullPath = "";
                string uploadPath = "";

                foreach (var formFile in files)
                {
                    string fileName = Path.GetFileName(formFile.FileName);// 原始文件名称
                    string fileExtension = Path.GetExtension(fileName).ToLower(); // 文件扩展名
                    string saveName = DateTime.Now.ToString("yyyyMMddhhmmssffff") + fileExtension; // 保存文件名称
                    int filesize = int.Parse(formFile.Length.ToString()) / 1024;//图片大小(KB)
                    if (filesize > 5120 || filesize <= 2 || (fileExtension != ".jpg" && fileExtension != ".png" && fileExtension != ".gif"))
                    {
                        return Json(new { Success = false, Message = "上传失败!\r请上传jpg/png/gif格式图片,文件大小不要超过5MB也不要小于2K" });
                    }
                    fullPath = Path.Combine(filePath, saveName);
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    using (var stream = new FileStream(fullPath, FileMode.CreateNew))
                    {
                        formFile.CopyTo(stream);
                    }
                    uploadPath = "/Uploads/Images/" + folder + "/" + saveName;
                }

                return Json(new { Success = true, FilePath = uploadPath });
            }
            catch (Exception e)
            {
                NLogHelper.WriteDebug("图片上传方法异常:"+ e.Message.ToString());
                return Json(new { Success = false, Message = e.Message.ToString() });
            }
           
        }

前端代码:

        <tr>
            <th class="formTitle">导航图片</th>
            <td class="formValue" colspan="3">
                <input type="file" id="picture_upload" name="picture_upload" value='导航图片' />
            </td>
        </tr>

<link href="~/scripts/plugins/uploadify/uploadify.css" rel="stylesheet" />
<script src="~/scripts/plugins/uploadify/jquery.uploadify.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#picture_upload').uploadify({
            uploader: '/Uploads/UploadImage?folder=ArticleCategory',
            swf: '/scripts/plugins/uploadify/uploadify.swf',
            buttonText: "请选择上传图片",
            height: 30,
            width: 120,
            //'fileExt': '*.jpg;*.gif,*.png',                 //允许上传的文件格式为*.jpg,*.gif,*.png
            //'fileDesc': 'Web Image Files(.JPG,.GIF,.PNG)',  //过滤掉除了*.jpg,*.gif,*.png的文件
            //'queueID': 'fileQueue',
            'sizeLimit': '5120000',                         //最大允许的文件大小为5M
            //'auto': false,                                  //需要手动的提交申请
            'multi': false,                                 //一次只允许上传一张图片
            'onUploadSuccess': function (file, data, response) {
                var obj = jQuery.parseJSON(data); //把返回的Json序列转化为obj对象
                if (obj.success) {
                    $('#PictureUrl').val(obj.filePath);
                }
                else
                    alert(obj.Message);
            }
        });
    });

</script>

猜你喜欢

转载自www.cnblogs.com/yechangzhong-826217795/p/12046622.html
今日推荐