zoukankan      html  css  js  c++  java
  • 文档库文件上传Webpart(原创)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using System.IO;
    using System.Web.UI.WebControls.WebParts;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.Workflow;

    namespace UploadDocument
    {
        public class AddFile : WebPart
        {

            private HtmlTableCell htmlTableCell = new HtmlTableCell();
            private Button SubmitButton = new Button();
            private FileUpload fileUpload = new FileUpload();
            SPSite site;
            SPWeb web;


            private void AddControl()
            {
                this.fileUpload.ID = "fileUpload";
                this.fileUpload.ToolTip = "上传文件";
                this.fileUpload.CssClass = "upload_input";
                this.htmlTableCell.Controls.Add(this.fileUpload);

                this.SubmitButton.ID = this.ID + "SubmitButton";
                this.SubmitButton.Text = "上传文件";
                this.SubmitButton.CssClass = "upload_edit_bt";
                this.SubmitButton.Click += new EventHandler(SubmitButton_Click);
                this.htmlTableCell.Controls.Add(this.SubmitButton);

            }

            void ClearButton_Click(object sender, EventArgs e)
            {
               
            }

            void SubmitButton_Click(object sender, EventArgs e)
            {
                try
                {
                    if (this.fileUpload.FileName == "") return;
                    string strRoot = root;

                    if (filePath != "")
                        strRoot = root + "/" + filePath;

                    if (this.Page.Request.QueryString["RootFolder"] != null)
                    {
                        strRoot = this.Page.Request.QueryString["RootFolder"];
                    }

                    SPFile docFile = AddFileByPath(strRoot, this.fileUpload.FileName, this.fileUpload.FileBytes);
                    string id = docFile.Item.UniqueId.ToString();
                    if (dirType == defaultDirType.直接地址)
                    {
                        this.Page.Response.Redirect(dirPageURL + "?ListName=" + libName + "&RootFolder=" + strRoot + "&id=" + id + "&fileName=" + this.fileUpload.FileName);
                    }
                    else if (dirType == defaultDirType.工作流)
                    {
                        SPList list = web.Lists[libName];

                        SPWorkflowAssociation wrkFl = list.WorkflowAssociations[0];
                        site.WorkflowManager.StartWorkflow(list.GetItemByUniqueId(new Guid(id)),wrkFl, wrkFl.AssociationData,true);

                        string taskid = list.GetItemByUniqueId(new Guid(id)).Workflows[0].Tasks[0].ID.ToString();

                        string wf_url = siteUrl+"/"+list.GetItemByUniqueId(new Guid(id)).Workflows[0].TaskList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + taskid;  
                        this.Page.Response.Redirect(wf_url);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


            //获取和设置站点的地址
            private const string defaultSiteUrl = "";
            private string siteUrl = defaultSiteUrl;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("站点地址(必填),如http://eiptest/")]
            [WebDescription("这里可以更换连接到文档库的地址")]
            public string SiteUrl
            {
                get
                {
                    return this.siteUrl;
                }
                set
                {
                    siteUrl = value;
                }
            }

            private const string defaultWebUrl = "";
            private string webUrl = defaultWebUrl;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("网站地址(可不填)")]
            [WebDescription("这里可以更换连接到文档库网站的地址")]
            public string WebUrl
            {
                get
                {
                    return this.webUrl;
                }
                set
                {
                    webUrl = value;
                }
            }

            //获取和设置文档库名
            private const string defaultLibName = "文档";
            private string libName = defaultLibName;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("文档库名(必填),如文档")]
            [WebDescription("这里可以更换文档库的名字")]
            public string DocLibraryName
            {
                get
                {
                    return this.libName;

                }
                set
                {
                    libName = value;
                }
            }

            //设定起始节点
            private const string defaultRoot = "/Documents";
            private string root = defaultRoot;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("起始节点(必填),如:/列表名/目录名")]
            [WebDescription("请输入起始节点的地址")]
            public string Root
            {
                get
                {
                    return root;
                }
                set
                {
                    if (value == "")
                    {
                        root = "/Documents";
                    }
                    else
                    {
                        root = value;
                    }
                }
            }

            //设定起始节点
            private const string defaultFilePath = "";
            private string filePath = defaultFilePath;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("目录名(可不填)")]
            [WebDescription("请输入目录名")]
            public string FilePath
            {
                get
                {
                    return filePath;
                }
                set
                {

                    filePath = value;
                }
            }

            //跳转方式
            public enum defaultDirType { 直接地址, 工作流 };
            private defaultDirType dirType = defaultDirType.直接地址;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("跳转方式")]
            [WebDescription("请输入跳转方式")]
            public defaultDirType DirType
            {
                get
                {
                    return dirType;
                }
                set
                {

                    dirType = value;
                }
            }


            //设定跳转页面
            private const string defaultDirPageURL = "";
            private string dirPageURL = defaultDirPageURL;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("跳转页面,如http://eip/Pages/XX.aspx")]
            [WebDescription("请输入跳转页面")]
            public string DirPageURL
            {
                get
                {
                    return dirPageURL;
                }
                set
                {

                    dirPageURL = value;
                }
           }

             //是否显示上传菜单
            public enum DisplayUploadMenu{ 显示,不显示};
            private DisplayUploadMenu uploadMenu = DisplayUploadMenu.不显示;

            [Personalizable]
            [WebBrowsable, Category("配置")]
            [WebDisplayName("是否显示上载菜单")]
            [WebDescription("是否显示上载菜单")]
            public DisplayUploadMenu UploadMenu
            {
                get
                {
                    return uploadMenu;
                }
                set
                {

                    uploadMenu = value;
                }
            }


            protected override void OnLoad(EventArgs e)
             {

                 base.OnLoad(e);
                 if (this.Page.IsPostBack == true) this.AddControl();

             }

            protected override void CreateChildControls()
            {
                this.Controls.Add(this.htmlTableCell);
                if (this.Page.IsPostBack == false) this.AddControl();
            }

            protected override void Render(HtmlTextWriter writer)
            {

                base.Render(writer);

            }

            protected override void OnPreRender(EventArgs e)
            {
                if (uploadMenu == DisplayUploadMenu.不显示)
                Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "js0001", "/wpresources/JS/HiddenMenu.js");

                CssRegistration cssControls = new CssRegistration();
                cssControls.Name = "/wpresources/style/feiyong/css/font.css";

                CssRegistration cssControls2 = new CssRegistration();
                cssControls2.Name = "/wpresources/style/feiyong/css/upload_edit.css";

                CssRegistration cssControls3 = new CssRegistration();
                cssControls3.Name = "/wpresources/style/feiyong/css/ad_metting_room.css";

                Page.Header.Controls.Add(cssControls);
                Page.Header.Controls.Add(cssControls2);
                Page.Header.Controls.Add(cssControls3);
            }

            private SPFile AddFileByPath(string RootFolder, string fileName, byte[] filebyte)
            {
                try
                {
                    site = new SPSite(siteUrl);
                   
                    if (webUrl != "")
                    {
                        web = site.OpenWeb(webUrl.Trim());
                    }
                    else
                    {
                        web = site.OpenWeb();
                    }

                    site.AllowUnsafeUpdates = true;
                    //SPWeb web = SPControl.GetContextWeb(Context);

                    web.AllowUnsafeUpdates = true;

                    SPList list = web.Lists[libName];
                    SPFolder rootFolder = web.GetFolder(RootFolder);
                    if (rootFolder.Exists)
                    {
                        //创建文件
                        byte[] binFile = filebyte;// readFile(filePath);
                        SPFile spFile = web.GetFile(rootFolder.Url + "/" + fileName);
                        if (spFile.Exists)
                        {
                            spFile.Delete();
                        }
                        //写入文件
                        web.AllowUnsafeUpdates = true;
                       
                        //优化的方法,一次提交
                        Hashtable ht=new Hashtable();
                        ht.Add("vti_title", fileName);
                        SPListItem item=rootFolder.Files.Add(rootFolder.Url + "/" + fileName, binFile, ht, true).Item;
                        string id = item.UniqueId.ToString();

                        //加入权限
                        //SharePointRole.addRole(site, web, item, "读取");
                       
                        web.AllowUnsafeUpdates = false;

                        return spFile;
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            private void addFolder(string checkPath, string createPath, string folderName, SPWeb web, SPList list)
            {
                SPFolder rFolder = web.GetFolder(checkPath);
                //判断是否存在文件夹,没有则新建
                if (!rFolder.Exists)
                {
                    SPListItem newFolder = list.Items.Add(createPath, SPFileSystemObjectType.Folder, folderName);
                    web.AllowUnsafeUpdates = true;
                    newFolder.Update();
                    web.AllowUnsafeUpdates = false;
                    rFolder = newFolder.Folder;
                }
            }

            private byte[] readFile(string fileName)
            {
                FileStream pFileStream = null;
                byte[] pReadByte = new byte[0];
                try
                {
                    pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    BinaryReader r = new BinaryReader(pFileStream);
                    r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                    pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                    return pReadByte;
                }
                catch
                {
                    return pReadByte;
                }
            }

            //写byte[]到fileName
            private bool writeFile(byte[] pReadByte, string fileName)
            {
                FileStream pFileStream = null;
                try
                {
                    pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
                    pFileStream.Write(pReadByte, 0, pReadByte.Length);
                }
                catch
                {
                    return false;
                }
                finally
                {
                    if (pFileStream != null)
                        pFileStream.Close();
                }
                return true;
            }

           

        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解
    40个Java多线程问题总结
    Windows10实用技巧-固定快捷方式到磁贴菜单方式
    wordpress初始化安装
    xshell输入奇怪,空格间距变大
    Python2和Python3共存安装
    搭建nginx反向代理用做内网域名转发
    下载网页视频音频方法(djyeye为例)
    Dell 戴尔预装Windows8改成Windows7
    Nginx基本功能极速入门
  • 原文地址:https://www.cnblogs.com/starcrm/p/1562666.html
Copyright © 2011-2022 走看看