zoukankan      html  css  js  c++  java
  • 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);
                }
            }

  • 相关阅读:
    【AtCoder】ARC075
    【BZOJ】3022: [Balkan2012]The Best Teams
    【Codeforces】Gym100633 D. LWDB
    MIME协议在邮件中的应用详解
    struts返回json数据
    mysql-存储过程(转载)
    安卓OKhttp请求封装
    安卓动态添加碎片
    通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)
    安卓原生与hml交互(WebView基础)
  • 原文地址:https://www.cnblogs.com/a546558309/p/2607058.html
Copyright © 2011-2022 走看看