码迷,mamicode.com
首页 > Windows程序 > 详细

ASP.NET Web API相关

时间:2017-09-20 14:55:46      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:save   create   datetime   exception   message   sage   image   log   foreach   

 

接收移动端上传的图片,示例代码: 

        [HttpPost]
        [ApiSecurityFilter]
        public IHttpActionResult UploadImg()
        {
            string imgs = "";
            try
            {
                System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                foreach (string key in files.AllKeys)
                {
                    System.Web.HttpPostedFile file = files[key];
                    if (!String.IsNullOrEmpty(file.FileName))
                    {
                        string filePath = "/Upload/Images/" +DateTime.Now.ToString("yyyyMM") + "/";
                        string dir = System.Web.HttpContext.Current.Server.MapPath(filePath);
                        if (!Directory.Exists(dir))
                            Directory.CreateDirectory(dir);
                        string oldName = file.FileName;
                        string ext = oldName.Substring(oldName.LastIndexOf("."));
                        string newName = DateTime.Now.ToString("ddHHmmssfff") + ext;
                        string url = filePath + newName;
                        file.SaveAs(dir + newName);
                        imgs += "网址" + url + ",";
                    }
                }

                //添加、更新到数据库

            }
            catch (Exception ex)
            {
                Log.Error("=========上传图片错误:" + ex.Message + "=============");
                return Json(new { Result = "fail", Response = ex.Message });
            }
            return Json(new { Result = "ok", Response = imgs.TrimEnd(,) });
        }

ASP.NET Web API相关

标签:save   create   datetime   exception   message   sage   image   log   foreach   

原文地址:http://www.cnblogs.com/dances/p/7560651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!