C#代码实现上传文件到SharePoint文档库

本文介绍如何使用C#代码实现将文件上传到SharePoint文档库的功能,包括代码实现步骤和实例演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    SharePoint的文档库是根据数据库虚拟出来的,以HTTP形式呈现,因要创建一个页面单独实现上传功能,故对于其存储和呈现机制进行了学习和研究,不过网络上相关资料还真是很少。SharePoint个人觉得还是比较适合不需要进行复杂逻辑功能的二次开发的网站构建,即适合一般基于office组件的功能门户,能够极大提高效率。

以下代码能够实现往文档库下层目录上传文件的功能,主要还是参考网络上其他文章

using  System.IO;
using  Microsoft.SharePoint;
using  System.Web;

namespace  ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"C:Documents and SettingsAdministrator桌面word_test.docx";
            System.IO.FileInfo myfile = new System.IO.FileInfo(fileName);               
            byte[] fileContents = new byte[int.Parse(myfile.Length.ToString())];
            FileStream fs=File.OpenRead(fileName);
            int n = fs.Read(fileContents, 0, int.Parse(myfile.Length.ToString()));
            string result = Program.UploadDocument("word_test.docx", fileContents, @"http://zpkxv7t0p3xxqyg:47024/DocLib1/New_Test");           
        }


        public static string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
        {
            if (fileContents == null)
            {
                return "Null Attachment";
            }

            try
            {
                
                int iStartIndex = pathFolder.LastIndexOf("/");               
                string sitePath = pathFolder.Remove(iStartIndex);
                string folderName = string.Empty;

                if (sitePath.LastIndexOf("/") > 6)
                {
                    int sec_iStartIndex = sitePath.LastIndexOf("/");
                    // System.Console.WriteLine(sec_iStartIndex);
                    sitePath = pathFolder.Remove(sitePath.LastIndexOf("/"));
                    folderName = pathFolder.Substring(sec_iStartIndex + 1);
                  //  System.Console.WriteLine(folderName);
                    // System.Console.WriteLine(folderName);
                    // System.Console.WriteLine(sitePath);
                }

                //   string folderName = pathFolder.Substring(iStartIndex + 1);
                //   System.Console.WriteLine(folderName);
                else
                {
                    folderName = pathFolder.Substring(iStartIndex + 1);
                   // System.Console.WriteLine(folderName);
                }

                SPSite site = new SPSite(sitePath);
                SPWeb web = site.OpenWeb();
                
               
                SPFolder folder = web.GetFolder(folderName);

                string fileURL = fileName;
                
                folder.Files.Add(fileURL, fileContents);

                if (folder.Files[fileURL].CheckedOutBy.Name != "")
                {
                    folder.Files[fileURL].CheckIn("File Checked In");
                }

               
                return "File added successfully!";
                
               
            }

            catch (System.Exception ex)
            {
                return ex.Source + ":" + ex.Message;
            }

        }

    }

}


转载于:https://www.cnblogs.com/Areas/archive/2011/12/06/2277588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值