Mvc3上传文件的方法封装

我们在做上传文件的时候,文件上传的逻辑是一样的,操作数据库的逻辑不一样,那我们怎么将这个功能统一封装起来呢,

因为上传文件的文件流为空和不为空的时候,操作是不一样的,我们只能将操作作为参数传进去,也就是下面的action1和action2,看下面的代码

首先看一下封装的代码

  /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileControlName">控件名称</param>
        /// <param name="filepath">文件夹路径</param>
        /// <param name="action1">上传成功后的操作</param>
        /// <param name="action2">文件流为空时的操作</param>
        /// <param name="allpath">文件全路径</param>
        public static JsonResult UploadFile(string fileControlName, string filrpath, Func<JsonResult> action1, Func<JsonResult> action2, out string allpath)
        {
            HttpPostedFile file = HttpContext.Current.Request.Files[fileControlName];
            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    var filename = Path.GetFileName(file.FileName);
                    var fileExtension = filename.Substring(filename.LastIndexOf("."));
                    var buildName = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                    var savepath = Path.Combine(HttpContext.Current.Server.MapPath(filrpath), buildName);
                    file.SaveAs(savepath);
                    allpath = savepath;
                    return action1();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                allpath = string.Empty;
                return action2();
            }
        }

然后来看一下调用封装方法的代码
  /// <summary>
        /// 更新文档
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public JsonResult UpdProDoc(ProjectDoc doc)
        {
            try
            {
                ProjectDoc finddoc = this.repository.Find<ProjectDoc>(P => P.Pid.Equals(doc.Pid));
                string savepath = string.Empty;
                Func<JsonResult> action1 = () =>
                {
                    finddoc.Url = savepath.Replace(@"\", "/");
                    finddoc.OperateTime = DateTime.Now;
                    finddoc.ProId = doc.ProId;
                    finddoc.Title = doc.Title;
                    finddoc.DocName = doc.DocName;
                    finddoc.Uid = doc.Uid;
                    finddoc.Description = doc.Description;
                    this.repository.Update(finddoc);
                    this.repository.SaveChanges();
                    return Json(new { success = true, Message = "文档更新成功!" }, "text/html", JsonRequestBehavior.AllowGet);
                };
                Func<JsonResult> action2 = () =>
                {
                    finddoc.OperateTime = DateTime.Now;
                    finddoc.ProId = doc.ProId;
                    finddoc.Title = doc.Title;
                    finddoc.DocName = doc.DocName;
                    finddoc.Uid = doc.Uid;
                    finddoc.Description = doc.Description;
                    this.repository.Update(finddoc);
                    this.repository.SaveChanges();
                    return Json(new { success = true, Message = "文档更新成功!" }, "text/html", JsonRequestBehavior.AllowGet);
                };
                return SysUtil.UploadFile("File", "~/Content/Upload/Prodocuments", action1, action2, out savepath);
            }
            catch (Exception)
            {
                return Json(new { success = false, Message = "文档更新失败!" }, "text/html", JsonRequestBehavior.AllowGet);
            }
        }

 

 

转载于:https://www.cnblogs.com/a546558309/archive/2012/07/24/2607058.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值