C# 导入图片

/// <summary>
        /// 图片上传  ActionResult   HttpPostedFileBase Files, 
        /// </summary>
        /// <param name="file"></param>
        ///<param name="structId">项目id</param>
        ///<param name="Type">项目下都传1</param> STRUCT
        /// <returns></returns>
        [AcceptVerbs("Post")]
        [LogInfo("图片上传", true)]
        public object UpImage(int structId, string type)
        {
            string msg = "";
            string FilePath = "";
            int n = 1;
            try
            {
                //上传图片格式数组声明
                string[] fileter = new string[] { ".jpg", ".png", ".jpeg" };
                //多文件
                System.Web.HttpFileCollection upFiles = System.Web.HttpContext.Current.Request.Files;

                if (upFiles.Count > 0 && upFiles.Count < 10)
                {
                    for (int i = 0; i < upFiles.Count; i++)
                    {
                        //第1张图片开始;
                        n += i;
                        HttpPostedFile PostedFile = upFiles[i];
                        if (PostedFile.ContentLength > 0)
                        {
                            //保存文件路径
                            string path = "/upload/image/" + DateTime.Now.ToString("yyyyMM");
                            //相对程序站点路径
                            path = System.Web.HttpContext.Current.Server.MapPath(path);
                            //上传文件后缀
                            string fileSuffix = PostedFile.FileName.Substring(PostedFile.FileName.LastIndexOf(".")).ToLower();
                            //保存图片名称以时间格式
                            string fileName = DateTime.Now.ToString("yyyyMMddhhmmssms") + fileSuffix;

                            if (fileter.Contains(fileSuffix))//&& Files.ContentLength <= 20480
                            {
                                //判断文件目录是否存在,如果不存在则创建目录
                                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                                if (!directoryInfo.Exists)
                                {
                                    directoryInfo.Create();
                                };
                                //保存图片
                                //Files.SaveAs(path + "/" + fileName);
                                //虚拟路径
                                FilePath = ConfigurationManager.ConnectionStrings["Url"].ConnectionString + "/upload/image/" + DateTime.Now.ToString("yyyyMM") + "/" + fileName;
                                //地址保存到数据库
                                string sql = string.Format(@"INSERT INTO T_DIM_COMMONIMAGE(Struct_Id,Path,ImageName,Type)values({0},‘{1}‘,‘{2}‘,‘{3}‘)", structId, FilePath, fileName, type);
                                SqlHelper.ExecteNonQueryText(sql);
                                msg += "第" + n + "张图片上传成功;";

                            }
                            else
                            {
                                msg += "第" + n + "张图片上传失败,只能上传jpg,png,jpeg 类型的图片;";
                            }
                        }
                        else
                        {
                            msg += "第" + n + "张,未选中图片;";
                        }
                    }

                }
                else
                {
                    msg+= "不能超过10张图片;";
                    return msg;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息" + ex.Message);
                msg = "上传失败";
            }
            return msg;
        }

  

C# 导入图片

上一篇:C# DataGridView删除指定行


下一篇:WPF 验证之IDataErrorInfo