Sharepoint API - 如何从ASP.NET Web应用程序上传文件到Sharepoint文档库

9
我是一名新手Sharepoint Server,我们有没有工具可以从ASP.NET应用程序上传文件。请问您能提供宝贵的答案吗?
1个回答

7
你可以编写一些自定义代码来实现它。如果你在同一台服务器上,可以使用SharePoint API,或者使用WebServices。
以下是示例代码,假设您知道文档库的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();
                    }
                }
            }
        }

你也可以使用SPFolder.Add(url, Stream, overwrite)来代替将整个文件读入内存(如果你计划上传大文件,这可能会导致性能问题)。 - Marek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接