Exemplo n.º 1
0
        public IHttpActionResult UploadImgageFromWeb(string ext)
        {
            ResultMsg resultMsg = new ResultMsg();
            //获取配置文件
            string UploadFormat   = System.Configuration.ConfigurationManager.AppSettings["UploadFormat"];
            string UploadFileSize = System.Configuration.ConfigurationManager.AppSettings["UploadFileSize"];

            // 获取传统context
            var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
            //图片名
            string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(99999);

            if (string.IsNullOrEmpty(ext))
            {
                resultMsg.ResultCode = -1;
                resultMsg.ResultMsgs = "上传失败,文件格式必须为" + UploadFormat + "类型";
                return(Ok(resultMsg));
            }
            ext = ext.ToLower();

            //检查文件格式
            string[] extArr = UploadFormat.Split(',');
            if (string.IsNullOrEmpty(ext) || !extArr.Contains(ext))
            {
                // HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent("文件格式不支持", Encoding.GetEncoding("UTF-8"), "text/plain") };
                resultMsg.ResultCode = -1;
                resultMsg.ResultMsgs = "上传失败,文件格式必须为" + UploadFormat + "类型";
                return(Ok(resultMsg));
            }

            try
            {
                string FileFullPath = context.Server.MapPath(string.Format("~/upload/{0}//", ext));

                //测试代码
                //string waterPath = "E:/Demo/接口/ImageServer/ImageServer/img/jpg/20181117/tz.png";

                //如果不存在就创建file文件夹
                if (!Directory.Exists(FileFullPath))
                {
                    Directory.CreateDirectory(FileFullPath);
                }
                //不同时间上传文件
                string FileTime = FileFullPath + DateTime.Now.ToString("yyyyMMdd") + $@"\";
                if (!Directory.Exists(FileTime))
                {
                    Directory.CreateDirectory(FileTime);
                }

                var filePath = FileTime + FileName + "." + ext;

                if (context.Request.Files.Count > 0)  //Request.Files 获取表单中的文件
                {
                    for (int i = 0; i < context.Request.Files.Count; i++)
                    {
                        HttpPostedFileBase hpf = context.Request.Files[i]; //这个对象是用来接收文件,利用这个对象可以获取文件的name path等
                        hpf.SaveAs(filePath);                              //用SaveAs保存到上面的路径中
                    }
                }
                if (ext != "zip")
                {
                    MakeThumbnail(filePath, filePath + "_small.jpeg", 150, 80);
                    NewAddWaterMark.AddTextWatermark(filePath, filePath + "_seal.jpeg", "版权专用", 14.0f, 120, ext);
                }
                string uploadfileurl = System.Configuration.ConfigurationManager.AppSettings.Get("ServerImgaes");
                string retAddr       = "/upload/" + ext + "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + FileName + "." + ext;

                resultMsg.ResultCode = 1;
                resultMsg.ResultMsgs = "上传成功";
                resultMsg.ResultData = retAddr;
                return(Ok(resultMsg));
            }
            catch (Exception ex)
            {
                WriteLog.WriteLogs(ex);
                resultMsg.ResultCode = -1;
                resultMsg.ResultMsgs = "上传失败";
                return(Ok(resultMsg));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="ImgPathEnum"></param>
        /// <param name="IsFullPath"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        private HttpResponseMessage UploadImgageFromAdmin(HttpContextBase context, int ImgPathEnum, bool IsFullPath, string ext)
        {
            // 获取传统context
            //context = (HttpContextBase)Request.Properties["MS_HttpContext"];
            Stream userfile = context.Request.InputStream;
            string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(99999);
            string FileExt  = context.Request.Headers["fileext"];

            if (!string.IsNullOrEmpty(FileExt))
            {
                FileExt = FileExt.ToLower();
            }
            else
            {
                FileExt = ext;
            }
            string[] extArr = new string[] { "gif", "jpg", "jpeg", "png", "ico" };
            if (string.IsNullOrEmpty(FileExt) || !extArr.Contains(FileExt))
            {
                HttpResponseMessage result = new HttpResponseMessage {
                    Content = new StringContent("图片格式不支持", Encoding.GetEncoding("UTF-8"), "text/plain")
                };
                return(result);
            }
            string uploadfolder  = context.Server.MapPath(string.Format("~/img/{0}/", ImgPathEnum));
            string uploadfileurl = System.Configuration.ConfigurationManager.AppSettings.Get("ServerImgaes");

            if (!Directory.Exists(uploadfolder))
            {
                Directory.CreateDirectory(uploadfolder);
            }
            FileExt = "." + FileExt;
            string retAddr = "";

            try
            {
                byte[]     buffer = new byte[256];
                int        len;
                string     FileFullPath = uploadfolder + FileName + FileExt;
                FileStream fs           = new FileStream(FileFullPath, FileMode.Create, FileAccess.Write);
                while ((len = userfile.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, len);
                }
                fs.Close();

                if (IsFullPath)
                {
                    retAddr = uploadfileurl + ImgPathEnum + "/" + FileName + FileExt;
                }
                else
                {
                    retAddr = FileName + FileExt;
                }
                MakeThumbnail(FileFullPath, FileFullPath + "_small.jpeg", 150, 80);
                NewAddWaterMark.AddTextWatermark(FileFullPath, FileFullPath + "_seal.jpeg", "版权专用", 14, 120, ext);
                HttpResponseMessage result = new HttpResponseMessage {
                    Content = new StringContent(retAddr, Encoding.GetEncoding("UTF-8"), "text/plain")
                };
                return(result);
            }
            catch (Exception ex)
            {
                HttpResponseMessage result = new HttpResponseMessage {
                    Content = new StringContent("上传出错:" + ex.Message, Encoding.GetEncoding("UTF-8"), "text/plain")
                };
                return(result);
            }
        }