Sharepoint的API - 如何从ASP.NET Web应用程序上传文件到SharePoint文档库
问题描述:
我是新的SharePoint服务器,我们是否有任何实用程序从ASP.NET应用程序上传文件。
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?
答
您可以编写一些自定义的code去做。如果你是在同一台服务器或使用Web服务您可以使用SharePoint API
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
下面是示例code假设你知道文档库的URL,您要上传的文件到根文件夹。你将不得不Microsoft.SharePoint.dll的添加为引用您的ASP.NET项目
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();
}
}
}
}