예제 #1
0
        // GET api/values/5
        public ApiRet Get(string id)
        {
            var client  = new RestClient("http://168.63.8.167:9200");
            var request = new RestRequest($"address/_search", Method.GET);

            request.AddParameter("q", id);
            request.AddParameter("pretty", "true");
            request.AddParameter("from", "0");
            request.AddParameter("size", "100");
            var res = client.Execute <RootObject>(request).Data;

            if (res == null)
            {
                return(null);
            }

            List <Citys> Citys = res.hits.hits.Select(x => new Code.Citys
            {
                Name = x._source.name,
                Id   = x._source.id
            }).ToList();

            ApiRet apiret = new ApiRet();

            apiret.Citys = Citys;
            apiret.Hits  = res.hits.hits.Count();

            return(apiret);
        }
예제 #2
0
        /// <summary>
        /// 上传Base64图片
        /// </summary>
        /// <param name="imgBase64String"></param>
        /// <returns></returns>
        public ResultInfo <string> UploadBase64Image(string imgBase64String)
        {
            if (!string.IsNullOrWhiteSpace(imgBase64String))
            {
                string[] base64Part = imgBase64String.Split(',');
                string   imgBase64  = base64Part.Length == 1 ? base64Part[0] : base64Part[1];

                string filename     = Guid.NewGuid() + ".jpg";
                byte[] fileBytes    = Convert.FromBase64String(imgBase64);
                ApiRet uploadResult = NetHelper.UploadFile(ConfigHelper.UploadFileUrl, ConfigHelper.UploadMoudleName, filename, fileBytes);
                if (uploadResult.RetCode == "0")
                {
                    return(new ResultInfo <string>(false, "上传成功", "0", uploadResult.Message.ToString()));
                }
                else
                {
                    //上传失败
                    return(new ResultInfo <string>(true, "上传失败", "-1", uploadResult.RetMsg));
                }
            }
            return(null);
        }
예제 #3
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="moudleName">文件存放目录</param>
        /// <param name="encoding">默认为gb2312</param>
        /// <param name="rename">为0时不重命名已上传文件的名称保存,不为0时文件名重命名为filename(若filename为空则用guid命名)</param>
        /// <param name="waterRemarkType">水印图片类型</param>
        /// <param name="filename">保存文件名</param>
        /// <returns></returns>
        protected WebApiRet UploadBase(string moudleName, string encoding = "", string rename = "", int waterRemarkType = 0, string filename = "")
        {
            WebApiRet ret = new WebApiRet();

            ret.RetCode = "0";

            long imgSize = 0;

            if (Request.Files.Count <= 0)
            {
                ret.RetCode = "-1";
                ret.RetMsg  = "请选择文件上传";
            }
            if (ret.RetCode == "0")
            {
                HttpFileCollectionBase files = Request.Files;
                HttpPostedFileBase     file  = files[0];//获取上传的文件

                int    hzIndex = file.FileName.LastIndexOf(".");
                string hz      = file.FileName.Substring(hzIndex + 1);

                if (ret.RetCode == "0")
                {
                    if (hz.ToLower() != "zip")
                    {
                        imgSize = file.InputStream.Length;
                        if (imgSize > ConfigHelper.MaxExcelProcessNum * 1024 * 1024)
                        {
                            ret.RetCode = "-3";
                            ret.RetMsg  = string.Format("文件不能超过{0}M,请选择其他图片上传", ConfigHelper.MaxExcelProcessNum);
                        }
                    }

                    if (ret.RetCode == "0")
                    {
                        int    fileLength = (int)file.InputStream.Length;
                        byte[] bytesRead  = new byte[(int)file.InputStream.Length];
                        Stream fileStream = file.InputStream;
                        fileStream.Read(bytesRead, 0, fileLength);
                        file.InputStream.Close();

                        if (!string.IsNullOrEmpty(filename) && !filename.EndsWith(hz))  //重命名文件名称无后缀时,添加后缀
                        {
                            filename = string.Concat(filename, ".", hz);
                        }
                        else
                        {
                            filename = file.FileName;
                        }

                        ApiRet uploadResult = NetHelper.UploadFile(ConfigHelper.UploadUrlNew, moudleName, filename, bytesRead, encoding, rename);

                        if (uploadResult.RetCode != "0")
                        {
                            ret.RetCode = "-4";
                            ret.RetMsg  = "上传失败";
                            ret.Message = uploadResult;
                        }
                        else
                        {
                            //ret.Message = ConfigHelper.StaticUrl + uploadResult.Message;
                            ret.Message = uploadResult.Message;
                        }
                    }
                }
            }
            return(ret);
        }