Sharepoint API - 如何从ASP.NET Web应用程序将文件上载到Sharepoint Doc Library

时间:2008-11-10 22:40:47

标签: asp.net sharepoint sharepoint-2007

我是Sharepoint Server的新手,我们是否有任何实用工具可以从ASP.NET应用程序上传文件。

您能否提供您宝贵的答案?

1 个答案:

答案 0 :(得分:7)

您可以编写一些自定义代码来执行此操作。如果您位于同一服务器上或使用WebServices

,则可以使用SharePoint API

下面是示例代码,假设您知道文档库的URL并且您正在将文档上载到根文件夹。您必须添加Microsoft.SharePoint.dll作为ASP.NET项目的参考

        using (SPSite siteCollection = new SPSite(url))
        {
            using (SPWeb spWeb = siteCollection.OpenWeb())
            {
                SPList spList = spWeb.GetList(url);

                string fileName = "XXXX";
                FileStream fileStream = null;
                Byte[] fileContent = null;

                try
                {
                    string docPath = XXXX; //physical location of the file
                    fileStream = File.OpenRead(docPath + fileName);
                    fileContent = new byte[Convert.ToInt32(fileStream.Length)];
                    fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));

                    spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + fileName, fileContent, true);
                    spList.Update();
                }
                catch(Exception ex)
                {

                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
        }