/// <summary>
        /// App上传图片
        /// </summary>
        /// <returns>返回上传图片的相对路径</returns>
        [HttpPost]
        public AppReturn<string> UploadImage()
        {
            AppReturn<string> rModel = new AppReturn<string>();
            //string result = "";
            // 检查是否是 multipart/form-data
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                InvestmentCommon.Log4NetHelper.Log.Error("不是有效的'form-data'类型");
                rModel.state = 0;
                rModel.msg = "不是有效的'form-data'类型";
                return rModel;
            }

DateTime dt = DateTime.Now;
            string path = string.Format("/imagestore/{0}/{1}{2}", dt.Year, dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'));
            string abtPath = HttpContext.Current.Server.MapPath(path);
            if (!InvestmentCommon.FileHelper.CreateDirectory(abtPath))
            {
                InvestmentCommon.Log4NetHelper.Log.Error(string.Format("创建目录{0}失败", abtPath));
                rModel.state = 0;
                rModel.msg = "创建图片目录失败";
                return rModel;
            }

string fileName = "";
            string ext = "";
            string filePath = "";
            try
            {
                HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
                HttpRequestBase request = context.Request;//定义传统request对象
                HttpFileCollectionBase imgFiles = request.Files;
                for (int i = 0; i < imgFiles.Count; i++)
                {
                    ext = InvestmentCommon.FileHelper.GetExtention(imgFiles[i].FileName);
                    fileName = string.Format("{0}{1}", System.Guid.NewGuid().ToString(), ext);
                    filePath = string.Format("{0}/{1}", path, fileName);
                    imgFiles[i].SaveAs(abtPath + "\\" + fileName);
                    imgFiles[i].InputStream.Position = 0;
                    rModel.data = filePath.Replace("/", "");
                    rModel.state = 1;
                    rModel.msg = "success";
                }

}
            catch (Exception e)
            {
                InvestmentCommon.Log4NetHelper.Log.Error("图片保存失败");
                rModel.state = 0;
                rModel.msg = "图片保存失败";
                return rModel;
            }

//result = Newtonsoft.Json.JsonConvert.SerializeObject(rList);
            return rModel;
        }

/// <summary>
        /// App上传图片
        /// </summary>
        /// <returns>返回上传图片的相对路径</returns>
        [HttpPost]
        public AppReturn<string> UploadImageByBase64(FileUp file)
        {
            AppReturn<string> rModel = new AppReturn<string>();
            if (string.IsNullOrEmpty(file.FileBase64))
            {
                InvestmentCommon.Log4NetHelper.Log.Error("没有选择要上传的图片");
                rModel.state = 0;
                rModel.msg = "没有选择要上传的图片";
                return rModel;
            }
            DateTime dt = DateTime.Now;
            string path = string.Format("/imagestore/{0}/{1}{2}", dt.Year, dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'));
            string abtPath = HttpContext.Current.Server.MapPath(path);
            if (!InvestmentCommon.FileHelper.CreateDirectory(abtPath))
            {
                InvestmentCommon.Log4NetHelper.Log.Error(string.Format("创建目录{0}失败", abtPath));
                rModel.state = 0;
                rModel.msg = "创建图片目录失败";
                return rModel;
            }

string fileName = "";
            string filePath = "";
            try
            {
                byte[] imgByte = Convert.FromBase64String(file.FileBase64);
                MemoryStream ms = new MemoryStream(imgByte);
                Image image = System.Drawing.Image.FromStream(ms);
                fileName = string.Format("{0}.png", System.Guid.NewGuid().ToString());
                filePath = string.Format("{0}/{1}", path, fileName);
                image.Save(abtPath + "\\" + fileName);
                rModel.data = filePath.Replace("/", "");
                rModel.state = 1;
                rModel.msg = "success";
                ms.Close();
                ms.Dispose();               
            }
            catch (Exception e)
            {
                InvestmentCommon.Log4NetHelper.Log.Error("图片保存失败");
                rModel.state = 0;
                rModel.msg = "图片保存失败";
                return rModel;
            }

return rModel;
        }

最新文章

  1. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
  2. FastJSON应用前测试--转载
  3. java 乱码问题-Dfile.encoding=UTF-8
  4. java中关于static的小知识
  5. SQLServer -- 递归查询树结构表
  6. NSIS脚本根据操作系统版本动态决定默认安装目录
  7. Hibernate 多对一
  8. grunt+bower依赖管理
  9. Java 冒泡排序的实现
  10. asp.net core 2.0 Microsoft.Extensions.Logging 文本文件日志扩展
  11. 用SPSS 画 人口金字塔(限SPSS 13.0以上)
  12. C语言之实现随机数产生算法
  13. angr进阶(2)C++程序的处理
  14. 执行sql脚本保留操作日志
  15. 使用Python批量修改文件名
  16. 连接hive
  17. Hibernate的状态
  18. VA&amp;RVA 和 RVA to RAW
  19. 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
  20. 设计模式之模板方法模式(Java实现)

热门文章

  1. Vue3 为何使用 Proxy 实现数据监听
  2. java_环境搭建、变量的使用
  3. docker找回构建时被删除的文件
  4. MySQL数据库——查询数据
  5. Flutter 容器(3) - AnimatedPadding
  6. 谈谈代码评审(code review)
  7. Hyperledger Fabric介绍
  8. 解决@ResponseBody不能和 &lt;mvc:annotation-driven&gt;同时使用的问题
  9. Unity3D天气系统插件UniStorm插件使用说明
  10. python设计模式之解释器模式