Sharepoint API - How to Upload files to Sharepoint Doc Library from ASP.NET Web Application

前端 未结 1 1907
攒了一身酷
攒了一身酷 2020-12-29 12:52

I am new to Sharepoint Server, Do we have any utility to upload files from ASP.NET application.

Could you please provide your valuable answers?

相关标签:
1条回答
  • 2020-12-29 13:37

    You can write some custom code to do it. You could use the SharePoint API if you are on the same server or use WebServices

    Here is the sample code assuming that you know the url of the document library and you are uploading the document to the root folder. You will have to add Microsoft.SharePoint.dll as reference to your ASP.NET project

            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();
                        }
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题