Ejemplo n.º 1
0
        public string SaveWeChatImage(string serverId)
        {
            var url = "http://api.weixin.qq.com/cgi-bin/media/get?access_token=" + BasicApi.GetAccessToken() + "&media_id=" + serverId;

            try
            {
                var    filename   = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                string uploadPath = System.Web.HttpContext.Current.Server.MapPath("/Upload/WeChatImage");

                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                FileStream file = new FileStream(uploadPath + "/" + filename, FileMode.Create, FileAccess.Write);//创建写入文件
                file.Close();

                var path = uploadPath + "/" + filename;

                var result = SaveImage(url, path);

                S3Unit.UploadFile("WeChatImage", path);
                if (result == "success")
                {
                    return("/WeChatImage/" + filename);
                }
                else
                {
                    return("Excute Error");
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public async Task <string> ImgUpload(string img_type)
        {
            string ROOT_PATH  = string.Empty;
            string ROOT_PATH2 = string.Empty;

            switch (img_type)
            {
            case "CaiPinKu":
                ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/CaiPinKu");
                break;

            case "Member":
                ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/Member");
                break;

            case "Product":
                ROOT_PATH  = HttpContext.Current.Server.MapPath("~/UploadFiles/Product");
                ROOT_PATH2 = HttpContext.Current.Server.MapPath("~/UploadFiles/SmallProduct");
                break;

            case "Cook":
                ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/Cook");
                break;

            case "Activity":
                ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/Activity");
                break;

            case "Tools":
                ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/Tools");
                break;

            case "Produce":
                ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/Produce");
                break;

            default:
                return("Request Error");
            }

            //ROOT_PATH = HttpContext.Current.Server.MapPath("~/UploadFiles/");

            List <Resource> resources = new List <Resource>();
            // 采用MultipartMemoryStreamProvider
            var provider = new MultipartMemoryStreamProvider();

            //读取文件数据
            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (var item in provider.Contents)
            {
                // 判断是否是文件
                if (item.Headers.ContentDisposition.FileName != null)
                {
                    //获取到流
                    var ms = item.ReadAsStreamAsync().Result;
                    //进行流操作
                    using (var br = new BinaryReader(ms))
                    {
                        if (ms.Length <= 0)
                        {
                            break;
                        }
                        //读取文件内容到内存中
                        var data = br.ReadBytes((int)ms.Length);
                        //Create
                        //当前时间作为ID
                        Resource resource = new Resource()
                        {
                            Id = DateTime.Now.ToString("yyyyMMddHHmmssffff", DateTimeFormatInfo.InvariantInfo)
                        };

                        //Info
                        FileInfo info = new FileInfo(item.Headers.ContentDisposition.FileName.Replace("\"", ""));
                        //文件类型
                        resource.Type = info.Extension.Substring(1).ToLower();
                        //Write
                        try
                        {
                            //文件存储地址
                            string dirPath = Path.Combine(ROOT_PATH);
                            if (!Directory.Exists(dirPath))
                            {
                                Directory.CreateDirectory(dirPath);
                            }

                            File.WriteAllBytes(Path.Combine(dirPath, resource.Id + "." + resource.Type), data);
                            resources.Add(resource);

                            //调用S3服务
                            ROOT_PATH += "\\" + resource.Id + "." + resource.Type;
                            S3Unit.UploadFile(img_type, ROOT_PATH);
                        }
                        catch (Exception ex)
                        {
                            return(ex.Message);
                        }
                        if (img_type == "Product")
                        {
                            ROOT_PATH  += "\\" + resource.Id + "." + resource.Type;
                            ROOT_PATH2 += "\\" + resource.Id + "." + resource.Type;
                            //return ROOT_PATH + "|" + ROOT_PATH2;

                            //SmallImage.SendSmallImage(ROOT_PATH, ROOT_PATH2, 200, 200, 100, "CUT");
                        }
                    }
                }
            }
            if (resources.Count == 0)
            {
                return("request error");
            }
            else if (resources.Count == 1)
            {
                return(JsonConvert.SerializeObject(resources.FirstOrDefault()));
            }
            else
            {
                return(JsonConvert.SerializeObject(resources));
            }
        }