代码比较简单,直接贴代码吧
/// <summary>
/// 文档库文档复制,获取文件
/// </summary>
public void getCopyDocLibFile(string IDstr)
{
try
{
//string IDstr = this.Request["id"];//文档ID
int ID = 0;
bool IsId = int.TryParse(IDstr, out ID);
//string filename = this.Request["fn"]; //文件夹名称
if (IsId)//&& !String.IsNullOrEmpty(filename)
{
string curDocLibUrl = "/Template/TemplateDocument/";//当前文档所在文档库
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Guid siteID = Web.Site.RootWeb.Site.ID;
using (SPSite spSite = new SPSite(siteID))
{
using (SPWeb spWeb = spSite.AllWebs["Template"])
{
spWeb.AllowUnsafeUpdates = true;
SPList clist = spWeb.GetList(curDocLibUrl);
SPListItem item = clist.GetItemById(ID);
if (item != null)
{
SPFile file = item.File;
string fn = System.IO.Path.GetFileName(file.Name);
byte[] contet = file.OpenBinary();
CopyDocLibFile(fn, contet);//复制文件
}
}
}
});
}
else
{
this.JResponse.RetCode = 2;
this.JResponse.Message = "获取id ,fn 失败!";
}
}
catch (Exception ex)
{
this.DebugTrace("lsycHandler.ashx DocLibFileCopy failed: {0}", ex.Message);
this.JResponse.RetCode = 2;
this.JResponse.Message = ex.Message;
}
}
/// <summary>
/// 复制文件到文档库根目录
/// </summary>
/// <param name="fn"></param>
/// <param name="filecontent"></param>
public void CopyDocLibFile(string fn, byte[] filecontent)
{
try
{
string docLibName = SPContext.Current.Web.CurrentUser.LoginName;
string sitetitle = "mydocs";//文档库所在子网站
Boolean isfileexist = false;
if (!String.IsNullOrEmpty(docLibName))
{
docLibName = docLibName.IndexOf("\\") >= 0 ? docLibName.Substring(docLibName.IndexOf("\\") + 1) : docLibName;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Guid siteID = Web.Site.RootWeb.Site.ID;
using (SPSite spSite = new SPSite(siteID))
{
using (SPWeb spWeb = spSite.AllWebs[sitetitle])
{
spWeb.AllowUnsafeUpdates = true;
SPList list = spWeb.Lists.TryGetList(docLibName);
SPDocumentLibrary docLib = (SPDocumentLibrary)list;
SPFolder sfolder = docLib.RootFolder;//文档库根目录
if (sfolder.Exists)
{
isfileexist = true;
SPFile f = sfolder.Files.Add(fn, filecontent, true);
SPListItem item = f.Item;
item.SystemUpdate();
}
}
}
});
}
int code = isfileexist ? 0 : 2;
string msg = isfileexist ? ("fn :" + fn) : "文件夹不存在!";
this.JResponse.RetCode = code;
this.JResponse.Message = msg;
}
catch (Exception ex)
{
this.DebugTrace("lsycHandler.ashx CopyDocLibFile failed: {0}", ex.Message);
this.JResponse.RetCode = 2;
this.JResponse.Message = ex.Message;
}
}
/// <summary>
/// 复制文件到指定文档库路径文件夹
/// </summary>
/// <param name="fn">文件名</param>
/// <param name="filecontent">二进制文件</param>
/// <param name="filename">文档库文件夹路径</param>
public void CopyDocLibtoFile(string fn, byte[] filecontent, string filename)
{
try
{
string docLibName = SPContext.Current.Web.CurrentUser.LoginName;
string sitetitle = "mydocs";//文档库所在子网站
Boolean isfileexist = false;
if (!String.IsNullOrEmpty(docLibName))
{
docLibName = docLibName.IndexOf("\\") >= 0 ? docLibName.Substring(docLibName.IndexOf("\\") + 1) : docLibName;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Guid siteID = Web.Site.RootWeb.Site.ID;
using (SPSite spSite = new SPSite(siteID))
{
using (SPWeb spWeb = spSite.AllWebs[sitetitle])
{
spWeb.AllowUnsafeUpdates = true;
SPFolder sfolder = spWeb.GetFolder(filename);//这边说明下filename = spWeb.ServerRelativeUrl + "/" + docLibName(文档库名称),具体结构可查看构建文档树那篇
if (sfolder.Exists)
{
isfileexist = true;
SPFile f = sfolder.Files.Add(fn, filecontent, true);
SPListItem item = f.Item;
item.SystemUpdate();
}
}
}
});
}
int code = isfileexist ? 0 : 2;
string msg = isfileexist ? ("fn :" + fn) : "文件路径【" + filename + "】不存在!";
this.JResponse.RetCode = code;
this.JResponse.Message = msg;
}
catch (Exception ex)
{
this.DebugTrace("lsycHandler.ashx CopyDocLibFile failed: {0}", ex.Message);
this.JResponse.RetCode = 2;
this.JResponse.Message = ex.Message;
}
}