예제 #1
0
        /// <summary>
        /// 图片上传地址
        /// </summary>
        /// <returns></returns>
        public ActionResult ImageUploadToAli(int checkModel)
        {
            string    _BImage  = string.Empty;
            string    _SImage  = string.Empty;
            string    _ImgGuid = Guid.NewGuid().ToString();
            Exception ex       = null;

            if (Request.Files.Count > 0)
            {
                var Imgfile = Request.Files[0];
                try
                {
                    var ext    = Path.GetExtension(Imgfile.FileName);
                    var client = new BaoYangYearCardController.WcfClinet <IFileUpload>();
                    var buffer = new byte[Imgfile.ContentLength];
                    Imgfile.InputStream.Read(buffer, 0, buffer.Length);

                    var _BytToImg = ImageConverter.BytToImg(buffer);
                    if (checkModel == 1)
                    {
                        if (Imgfile.ContentLength >= 1024 * 256)
                        {
                            ex = new Exception("图片大小不能超过256k");
                        }
                        else
                        {
                            int height;
                            if (int.TryParse(_BytToImg?["Height"].ToString(), out height) &&
                                height >= 256)
                            {
                                ex = new Exception("图片高度最大256dp");
                            }
                        }
                    }


                    if (ex == null && _BytToImg != null && _BytToImg.Count > 0)
                    {
                        _ImgGuid = string.Format(_ImgGuid + "${0}w_{1}h", _BytToImg["Width"], _BytToImg["Height"]);
                        _BImage  = WebConfigurationManager.AppSettings["UploadDoMain_image_push"] + _ImgGuid + ext;

                        client.InvokeWcfClinet(w => w.UploadImage(_BImage, buffer, 0, 0));
                    }
                }
                catch (Exception error)
                {
                    ex = error;
                }
            }
            return(Json(new
            {
                FullImage = WebConfigurationManager.AppSettings["DoMain_image"] + _BImage,
                PathImage = _BImage,
                Msg = ex?.Message ?? "上传成功"
            }, "text/html"));
        }