上传文件目录到share point

 

在下面的例子中,创建了一个自定义的Web part(关于如何创建WebPart,可以查看前面两节的内容),在这个web part中,添加了一个按钮,点击这个按钮,会将本地机某个目录下的文件及文件夹全部(采用递归方式)上传到Share Point中某一页面下的Docment Library(suhua doc library)中。下面是这个web part的主要代码:

using System;

using System.Collections.Generic;

using System.Text;

 

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

 

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebPartPages;

using Microsoft.SharePoint.WebControls;

using System.Xml.Serialization;

using System.Web.UI.HtmlControls;

 

using System.IO;

 

namespace SuhuaSample3

{

    public class MySample : WebPart

    {      

        private Button browserFolderBtn;

        public MySample()

        {           

            browserFolderBtn = new Button();

            browserFolderBtn.Click += new EventHandler(browserFolderBtn_Click);         

 

             this.Controls.Add(browserFolderBtn);

            browserFolderBtn.Text = "Browser a folder in suhua doc library";

 

        }  

        void browserFolderBtn_Click(object sender, EventArgs e)

        {

            try

            {

                SPSite siteCollection = SPControl.GetContextSite(Context);

                SPList list = siteCollection.AllWebs["TestSite/suhua"].Lists["suhua doc library"];

                SPDocumentLibrary doclib = (SPDocumentLibrary)list;

                //要把TestFolder目录下的所有文件及文件夹上传到suhua doc library中     

                RecursiveFolder(@"E:/Documents and Settings/suhuaan/TestFolder", doclib.RootFolder);

              

            }

            catch(Exception exp)

            {

                this.Controls.Add(new LiteralControl("<br/>" + "Add Folder Exception:" + exp.Message));

            }          

 

        }       

       

        //recursive

        /// <summary>

        /// recursive to add directory and files into spfolder

        /// </summary>

        /// <param name="strSrcPath">the source directory</param>

        /// <param name="curSPFolder">the folder item in share point docment library</param>

        protected void RecursiveFolder(string strSrcPath, SPFolder curSPFolder)

        {

            if (!Directory.Exists(strSrcPath))

            {

                return;

            }

 

            //add sub directories

            string[] subDics = Directory.GetDirectories(strSrcPath);

            if (subDics.Length > 0)

            {

                foreach (string dic in subDics)

                {

                    //get direcroty name

                    string dicName = dic.Substring(dic.LastIndexOf('//') + 1);

                    //add to share point

                    SPFolder addfolder = curSPFolder.SubFolders.Add(dicName);

                    SPListItem addItem = addfolder.Item;

                    addItem.Update();

 

                    RecursiveFolder(dic, addfolder);

                }

            }

 

            //add files

            string[] fileNames = Directory.GetFiles(strSrcPath);

            if (fileNames.Length > 0)

            {

                foreach (string filepath in fileNames)

                {

                    //get file name only, not path

                    string realname = filepath.Substring(filepath.LastIndexOf('//') + 1);

                    //add to share point

                    FileStream fileStream = File.OpenRead(filepath);

                    byte[] getbytes = new byte[fileStream.Length];

                    fileStream.Read(getbytes, 0, (int)fileStream.Length);

 

                    this.Controls.Add(new LiteralControl("<br/>" + curSPFolder.Url));

                    curSPFolder.Files.Add(realname, getbytes);

                }

            }

        }

    }

 

}

在PowerPages中,下载SharePoint文件通常是通过调用SharePoint Web Services API或者利用其内置的Microsoft Office SharePoint Foundation (MOSS) Web Services来完成的。这里是一个基本的步骤: 1. **设置环境**: 确保你已经安装了SharePoint的SOAP服务客户端库,例如Windows SharePoint Services SDK或SharePoint Server Management Shell工具。 2. **访问SharePoint站点**: 需要使用SharePoint的对象模型(如SPSite、SPList和SPFile)来与SharePoint交互。首先,你需要凭据(如用户名和密码)获取对SharePoint网站的访问权限: ```powerpages Dim spSite As SPSite Dim spWeb As SPWeb Set spSite = New SPSite("http://your-sharepoint-site") Set spWeb = spSite.OpenWeb() ``` 3. **定位文件**: 根据文件路径或列表ID找到你要下载的文件: ```powerpages Dim spList As SPSList Dim spFile As SPFile Set spList = spWeb.Lists("Documents") ' 替换为你的文档列表名 Set spFile = spList.Items.ItemById("file-id") ' 或者 spList.GetFileByServerRelativeUrl("/your-file-path") ``` 4. **下载文件**: 使用`spFile.OpenBinaryStream`方法读取文件内容,然后将其写入HTTP响应以便下载: ```powerpages Dim binaryStream As Stream binaryStream = spFile.OpenBinaryStream() Response.ContentType = "application/octet-stream" Response.AddHeader("Content-Disposition", "attachment; filename=your-file-name") ' 替换为文件的实际名称 Response.BinaryWrite(binaryStream.ReadAll()) binaryStream.Close() ``` 5. **清理资源**: 关闭流和对象以释放内存: ```powerpages binaryStream.Close() Set binaryStream = Nothing Set spFile = Nothing spWeb.Dispose() spSite.Dispose() ``` 注意:以上代码示例是理论上的,实际使用时需要处理异常,以及可能的安全限制,比如权限控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值