public async Task <IActionResult> UploadFile()
        {
            var file = Request.Form.Files.FirstOrDefault();

            if (file == null)
            {
                return(JsonContent(new { status = "error" }.ToJson()));
            }

            string fileNmae = file.FileName;
            Stream stream   = file.OpenReadStream();

            if (stream == null || string.IsNullOrEmpty(fileNmae))
            {
                return(JsonContent(new { status = "error" }.ToJson()));
            }
            string file_extension = Path.GetExtension(fileNmae);
            string image_url      = await FastDFSHelper.UpdateFile(stream, file_extension);

            var res = new
            {
                name     = file.FileName,
                status   = "done",
                thumbUrl = image_url,
                url      = image_url
            };

            return(JsonContent(res.ToJson()));
        }
Beispiel #2
0
        public async Task <IActionResult> UploadBase64([FromBody] JObject data)
        {
            var imageBase64 = data["image_base64"].ToObject <string>();
            var fileName    = data["file_name"].ToObject <string>();

            if (!string.IsNullOrWhiteSpace(imageBase64))
            {
                var reg = new Regex("data:image/(.*);base64,");

                imageBase64 = reg.Replace(imageBase64, "");

                byte[] imageByte = Convert.FromBase64String(imageBase64);

                var fileExt = fileName.Split('.').Reverse().FirstOrDefault();

                try
                {
                    var url = await FastDFSHelper.UploadAsync(imageByte, fileExt);

                    var rlt = MyDynamicResult(true, "");
                    rlt.url = url;

                    var img = BytesToImage(imageByte);

                    //var imgSM = SizeImageWithOldPercent(img, 100, 100);

                    //var byteSM = ImageToBytes(imgSM, fileExt);

                    //var urlSM = await FastDFSHelper.UploadAsync(byteSM, fileExt);

                    //rlt.urlSM = urlSM;

                    return(Ok(rlt));
                } catch (Exception e)
                {
                    var rlt = MyDynamicResult(false, "网络错误,请重试");

                    return(BadRequest(rlt));
                }
            }
            else
            {
                var rlt = MyDynamicResult(false, "图片不能为空");

                return(BadRequest(rlt));
            }
        }
        /// <summary>
        /// 上传文件到文件系统服务器
        /// </summary>
        /// <param name="uploadType">图片字段</param>
        /// <param name="fileBase64"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public AjaxResult UploadFileToServer(string uploadType, string id, string multiple, string fileBase64, string fileName)
        {
            string name = FastDFSHelper.UploadFile(fileBase64, fileName);

            if (name.IsNullOrEmpty())
            {
                return(Error("上传失败"));
            }
            dynamic obj = null;
            var     _ProductBusiness              = new ProductBusiness();
            var     _product_tagBusiness          = new product_tagBusiness();
            var     _DictionaryBusiness           = new DictionaryBusiness();
            var     _product_IntroductionBusiness = new product_IntroductionBusiness();

            switch (uploadType)
            {
            case "images":
            case "logo":
            case "photooffarmers":
            case "commodity_photo":
                obj = (product)_ProductBusiness.GetEntity(id.ToInt());
                break;

            case "img_url":
                obj = (product_tag)_product_tagBusiness.GetEntity(id);
                break;

            case "dicimages":
                obj = (dictionary)_DictionaryBusiness.GetEntity(id);
                break;

            default:
                obj = (product_Introduction)_product_IntroductionBusiness.GetTheData(uploadType);
                break;
            }
            if (obj != null)
            {
                switch (uploadType)
                {
                case "images":    //产品图片
                    obj.images = multiple == "0" ? name : (string.IsNullOrEmpty(obj.images) ? name : obj.images + "," + name);
                    _ProductBusiness.UpdateAny(obj, new List <string>()
                    {
                        "images"
                    });
                    break;

                case "logo":    //logo
                    obj.logo = multiple == "0" ? name : (string.IsNullOrEmpty(obj) ? name : obj.logo + "," + name);
                    _ProductBusiness.UpdateAny(obj, new List <string>()
                    {
                        "logo"
                    });
                    break;

                case "photooffarmers":    //农户照片
                    obj.photooffarmers = multiple == "0" ? name : (string.IsNullOrEmpty(obj.photooffarmers) ? name : obj.photooffarmers + "," + name);
                    _ProductBusiness.UpdateAny(obj, new List <string>()
                    {
                        "photooffarmers"
                    });
                    break;

                case "commodity_photo":    //扶贫商品照片
                    obj.commodity_photo = multiple == "0" ? name : (string.IsNullOrEmpty(obj.commodity_photo) ? name : obj.commodity_photo + "," + name);
                    _ProductBusiness.UpdateAny(obj, new List <string>()
                    {
                        "commodity_photo"
                    });
                    break;

                case "img_url":    //热门城市logo
                    obj.img_url = multiple == "0" ? name : (string.IsNullOrEmpty(obj.img_url) ? name : obj.img_url + "," + name);
                    _product_tagBusiness.UpdateAny(obj, new List <string>()
                    {
                        "img_url"
                    });
                    break;

                case "dicimages":    //数据字典照片
                    obj.images = multiple == "0" ? name : (string.IsNullOrEmpty(obj.images) ? name : obj.images + "," + name);
                    _DictionaryBusiness.UpdateAny(obj, new List <string>()
                    {
                        "images"
                    });
                    break;

                default:
                    obj.picture = multiple == "0" ? name : (string.IsNullOrEmpty(obj.picture) ? name : obj.picture + "," + name);
                    _product_IntroductionBusiness.UpdateAny(obj, new List <string>()
                    {
                        "picture"
                    });
                    break;
                }
            }
            return(Success(new ImagesData()
            {
                ImageName = name
            }));
        }