首页 > Windows开发 > 详细

ASP.NET Web API相关

时间:2017-09-20 13:34:39      阅读:246      评论:0      收藏:0      [点我收藏+]

 

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

        [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相关

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!