【问题标题】:Sharepoint API - How to Upload files to Sharepoint Doc Library from ASP.NET Web ApplicationSharepoint API - 如何从 ASP.NET Web 应用程序将文件上传到 Sharepoint 文档库
【发布时间】:2010-09-21 17:18:16
【问题描述】:

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

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

【问题讨论】:

    标签: asp.net sharepoint sharepoint-2007


    【解决方案1】:

    您可以编写一些自定义代码来执行此操作。如果您在同一台服务器上,则可以使用 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) 代替将整个文件读入内存(如果您打算上传大文件可能会导致性能问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2013-06-15
    • 1970-01-01
    • 2013-12-12
    • 2013-10-08
    相关资源
    最近更新 更多