在其他应用程序上传文件到MOSS文档库的例子
有时候我们需要在其他应用系统里把该系统的文档上传到MOSS文档库中,下面是一个例子,没技术含量,供大家参考。
Code
using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
public partial class FileToMOSSDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// upload files to moss document library
protected void Button1_Click(object sender, EventArgs e)
{
string SPSiteURL = ConfigurationManager.AppSettings["SPSiteURL"].ToString(); ;
string webName = ConfigurationManager.AppSettings["webName"].ToString(); ;
string documentLibraryName = ConfigurationManager.AppSettings["documentLibraryName"].ToString(); ;
string sourceFolder = ConfigurationManager.AppSettings["sourceFolder"].ToString(); ;
UpLoadToMOSS(SPSiteURL, webName, documentLibraryName, sourceFolder);
}
/**//// <summary>
///
/// </summary>
/// <param name="SPSiteURL">sharepoint site name</param>
/// <param name="webName">web name</param>
/// <param name="documentLibraryName">document library name</param>
/// <param name="sourceFolder">to be upload file's folder,example /upload</param>
private void UpLoadToMOSS(string SPSiteURL, string webName, string documentLibraryName, string sourceFolder)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spsite = new SPSite(SPSiteURL))
{
spsite.AllowUnsafeUpdates = true;
using (SPWeb web = spsite.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPDocumentLibrary doclib = (SPDocumentLibrary)web.Lists[documentLibraryName];
string[] files = Directory.GetFiles(base.Server.MapPath(Request.ApplicationPath) + sourceFolder);
FileStream upFile = null;
string newFileName = string.Empty;
string oldFileName = string.Empty;
foreach (string file in files)
{
upFile = new FileStream(file, FileMode.Open);
byte[] fileContent = new byte[file.Length];
upFile.Read(fileContent, 0, (int)file.Length);
//produce new filename in order to avoid same filename
oldFileName = file.Substring(file.LastIndexOf(@"\") + 1);
string[] fileName = oldFileName.Split('.');
newFileName = fileName[0].ToString() + "_" + Guid.NewGuid().ToString().Substring(0,10);
doclib.RootFolder.Files.Add(newFileName + "." + fileName[1].ToString(), fileContent);
}
upFile.Close();
}
}
}
);
}
}
using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
public partial class FileToMOSSDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// upload files to moss document library
protected void Button1_Click(object sender, EventArgs e)
{
string SPSiteURL = ConfigurationManager.AppSettings["SPSiteURL"].ToString(); ;
string webName = ConfigurationManager.AppSettings["webName"].ToString(); ;
string documentLibraryName = ConfigurationManager.AppSettings["documentLibraryName"].ToString(); ;
string sourceFolder = ConfigurationManager.AppSettings["sourceFolder"].ToString(); ;
UpLoadToMOSS(SPSiteURL, webName, documentLibraryName, sourceFolder);
}
/**//// <summary>
///
/// </summary>
/// <param name="SPSiteURL">sharepoint site name</param>
/// <param name="webName">web name</param>
/// <param name="documentLibraryName">document library name</param>
/// <param name="sourceFolder">to be upload file's folder,example /upload</param>
private void UpLoadToMOSS(string SPSiteURL, string webName, string documentLibraryName, string sourceFolder)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spsite = new SPSite(SPSiteURL))
{
spsite.AllowUnsafeUpdates = true;
using (SPWeb web = spsite.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPDocumentLibrary doclib = (SPDocumentLibrary)web.Lists[documentLibraryName];
string[] files = Directory.GetFiles(base.Server.MapPath(Request.ApplicationPath) + sourceFolder);
FileStream upFile = null;
string newFileName = string.Empty;
string oldFileName = string.Empty;
foreach (string file in files)
{
upFile = new FileStream(file, FileMode.Open);
byte[] fileContent = new byte[file.Length];
upFile.Read(fileContent, 0, (int)file.Length);
//produce new filename in order to avoid same filename
oldFileName = file.Substring(file.LastIndexOf(@"\") + 1);
string[] fileName = oldFileName.Split('.');
newFileName = fileName[0].ToString() + "_" + Guid.NewGuid().ToString().Substring(0,10);
doclib.RootFolder.Files.Add(newFileName + "." + fileName[1].ToString(), fileContent);
}
upFile.Close();
}
}
}
);
}
}