Exemple #1
0
        /// <summary>
        /// 上传产品图片
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public JsonResult UploadProductImg(int productID)
        {
            LayStateModel state = AttachmentMgr.Mgr.AddAttachment(HttpContext.Request.Files, productID
                                                                  , "product", ContextObjects.ProductImagePath);

            return(Json(state));
        }
Exemple #2
0
        /// <summary>
        /// 上传文件,目前只考虑单个文件上传
        /// </summary>
        /// <param name="fileBase">客户端上传的文件</param>
        /// <param name="relatedID">关联主键id</param>
        /// <param name="attachType">附件资源类型</param>
        /// <param name="backPath">上一级目录 ContextObjects类中配置,TODO仍需优化 </param>
        /// <returns></returns>
        public LayStateModel AddAttachment(HttpFileCollectionBase fileBase, int relatedID, string attachType, string backPath)
        {
            LayStateModel state = new LayStateModel();

            if (fileBase.Count == 0)
            {
                state.code = -1;
                state.msg  = "未能接受到上传文件,请重新检查上传";
                return(state);
            }
            else
            {
                HttpPostedFileBase file = fileBase[0];

                // 用户端上传的文件名称
                string fileName = file.FileName;

                // 校验服务端保存的路径是否存在
                if (!Directory.Exists(backPath))
                {
                    Directory.CreateDirectory(backPath);
                }

                // 修改文件名称为用户id,使用guid作为文件名称保存,防止文件名称冲突
                string extension = Path.GetExtension(fileName);
                string fullName  = Path.Combine(backPath, Guid.NewGuid() + extension);

                // 另存为至服务器上指定路径
                file.SaveAs(fullName);

                Attachment entity = new Attachment();
                entity.AttName   = fileName;
                entity.AttPath   = fullName;
                entity.AttType   = attachType;
                entity.UserID    = ContextObjects.CurrentUser.ID;
                entity.RelatedID = relatedID;
                entity.UpTime    = DateTime.Now;

                if (!Insert(entity))
                {
                    state.code = -1;
                    state.msg  = "上传成功,但更新失败,请重新上传!";
                }
                else
                {
                    // 返回主键id和全路径名称
                    state.data = new { src = fullName, id = entity.ID };
                    state.msg  = "上传成功!";
                }
                return(state);
            }
        }//AddAttachment end
Exemple #3
0
        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <returns></returns>
        public JsonResult SaveUserImg()
        {
            // 获取上传的文件
            var           files = HttpContext.Request.Files;
            LayStateModel state = new LayStateModel();

            if (files.Count == 0)
            {
                state.code = -1;
                state.msg  = "未能接受到上传文件,请重新检查上传";
            }
            else
            {
                HttpPostedFileBase file = files[0];

                // 用户端上传的文件名称
                string fileName = file.FileName;

                // 校验服务端保存的路径是否存在
                if (!Directory.Exists(ContextObjects.UserImagePath))
                {
                    Directory.CreateDirectory(ContextObjects.UserImagePath);
                }

                // 修改文件名称为用户id,否则容易造成上传文件名称冲突。或者使用guid作为文件名称保存
                string extension = Path.GetExtension(fileName);
                string fullName  = Path.Combine(ContextObjects.UserImagePath, ContextObjects.CurrentUser.UserID + extension);

                // 另存为至服务器上指定路径
                file.SaveAs(fullName);
                state.data = new { src = fullName };
                if (!UpdateHeadImage(fullName))
                {
                    state.code = -1;
                    state.msg  = "图片上传成功,但更新失败,请重新上传!";
                }
            }

            return(Json(state));
        }