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);
                }
            }

  • 相关阅读:
    慎用const_cast
    python中string的操作函数
    C++ Const总结
    python dict sorted 排序
    "没有找到MSVCP80D.dll,因此这个应用程序未能启动。重新安装应用程序...
    提高你开发效率的十五个 Visual Studio 使用技巧
    一些 python 插件 编译安装的注意事项
    html 制作表格 合并 样式
    将一个表的数据更新到另一个表中
    屏蔽web页面的右键,但不屏蔽输入框中的右键
  • 原文地址:https://www.cnblogs.com/a546558309/p/2607058.html
Copyright © 2011-2022 走看看