示例#1
0
        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Image GetPicture(string url, string photoHead, int photoTail, string forecastFilesHead)
        {
            Image img;

            try
            {
                ReqMeteoPic reqWavePic = new ReqMeteoPic();
                reqWavePic.photoHead         = photoHead;
                reqWavePic.photoTail         = photoTail;
                reqWavePic.forecastFilesHead = forecastFilesHead;
                HttpWebRequest req = WebRequest.CreateHttp(url);
                req.Method      = "POST";
                req.ContentType = "application/json;charset=UTF-8";
                string jsonStr = JsonConvert.SerializeObject(reqWavePic);
                byte[] payload = Encoding.UTF8.GetBytes(jsonStr);
                req.ContentLength = payload.Length;
                Stream writer = req.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                var    response = req.GetResponse() as HttpWebResponse;
                Stream stm      = response.GetResponseStream();
                img = Image.FromStream(stm);
                stm.Close();
            }
            catch
            {
                return(null);
            }
            return(img);
        }
        //[Route("v1/file/{subpath}/{filename}")]
        public async Task <HttpResponseMessage> MeteoPicture(ReqMeteoPic reqWavePic)
        {
            string stateStr = "";

            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "\\DailyData\\Meteorological\\Pictures\\" + DateTime.Today.ToString("yyyyMMdd") + "\\" + reqWavePic.forecastFilesHead + "\\" + reqWavePic.photoHead + reqWavePic.photoTailStr() + ".png";
                if (!File.Exists(path))
                {
                    path     = AppDomain.CurrentDomain.BaseDirectory + "NoPicture.png";
                    stateStr = path;
                }
                FileStream stream = new FileStream(path, FileMode.Open);

                HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StreamContent(stream)
                };
                resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = Path.GetFileName(path)
                };
                string contentType = MimeMapping.GetMimeMapping(path);
                resp.Content.Headers.ContentType   = new MediaTypeHeaderValue(contentType);
                resp.Content.Headers.ContentLength = stream.Length;

                return(await Task.FromResult(resp));
            }
            catch (Exception e)
            {
                CommonTools.WriteLog("获取图片异常:" + stateStr + "\r\n" + e.Message);
            }
            return(new HttpResponseMessage(HttpStatusCode.NoContent));
        }