在SharePoint中,经常需要将文档库中的文件进行压缩下载。为了实现这一功能,可以创建一个自定义的UI操作,允许用户将文档库中的项目打包成压缩文件并下载。这项功能可以让用户选择下载所有文件,也可以只下载选定视图中的文件,甚至可以选择是否包含文件的版本信息。
以下是实现文档库压缩下载功能的主要特点:
在SharePoint文档库的“操作”菜单中,添加了一个新的菜单项:
点击菜单项后,用户可以选择下载压缩文件,包括文件的版本信息。
首先,需要创建一个新的特性(Feature),定义一个新的SharePoint自定义UI操作。以下是XML配置文件的示例:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="DownloadZippedItems.MenuLink" Location="Microsoft.SharePoint.StandardMenu" GroupId="ActionsMenu" ControlAssembly="MZaki.CustomActions.DownloadZippedItems, Version=1.0.0.0, Culture=neutral, PublicKeyToken=da6289be64eaeba3" ControlClass="MZaki.CustomActions.DownloadZippedItems">
</CustomAction>
</Elements>
在这个自定义操作中,使用了一个控制程序集(Control Assembly),而不是重定向到另一个URL。这个程序集负责渲染菜单及其子菜单,并处理回发事件:
protected override void CreateChildControls()
{
if (!this.ChildControlsCreated)
{
base.CreateChildControls();
// 创建子菜单项
SubMenuTemplate mnuZipListItems = new SubMenuTemplate();
mnuZipListItems.Text = "Zip List Items";
mnuZipListItems.ImageUrl = "/_layouts/images/TBSPRSHT.GIF";
mnuZipListItems.Description = "Zip and download List Items";
mnuZipListItems.ID = "downloadZipped";
// 添加压缩并下载所有
PostBackMenuItemTemplate mnuListItem = new PostBackMenuItemTemplate();
mnuListItem.ID = "menu1";
mnuListItem.Text = "All Items";
mnuListItem.Description = "Zip and Download All Items";
mnuListItem.OnPostBack += new EventHandler(eventargs)(mnuListItem_OnPostBack);
// 添加压缩并下载所有含版本
PostBackMenuItemTemplate mnuListItem2 = new PostBackMenuItemTemplate();
mnuListItem2.Text = "All Items with Versions";
mnuListItem2.Description = "Zip and Download All Items with Versions";
mnuListItem2.ID = "menu2";
mnuListItem2.OnPostBack += new EventHandler(eventargs)(mnuListItem2_OnPostBack);
// 分隔符
MenuSeparatorTemplate separator = new MenuSeparatorTemplate();
// 当前视图
PostBackMenuItemTemplate mnuListItemCurrentView = new PostBackMenuItemTemplate();
mnuListItemCurrentView.Text = "Items In Current View";
mnuListItemCurrentView.Description = "Zip and Download All Items In Current View";
mnuListItemCurrentView.ID = "menu3";
mnuListItemCurrentView.OnPostBack += new EventHandler(eventargs)(mnuListItemCurrentView_OnPostBack);
// 当前视图含版本
PostBackMenuItemTemplate mnuListItemCurrentViewVersions = new PostBackMenuItemTemplate();
mnuListItemCurrentViewVersions.Text = "Items In Current View With Versions";
mnuListItemCurrentViewVersions.Description = "Zip and Download All Items In Current View With Versions";
mnuListItemCurrentViewVersions.ID = "menu4";
mnuListItemCurrentViewVersions.OnPostBack += new EventHandler(eventargs)(mnuListItemCurrentViewVersions_OnPostBack);
mnuZipListItems.Controls.Add(mnuListItem);
mnuZipListItems.Controls.Add(mnuListItem2);
mnuZipListItems.Controls.Add(separator);
mnuZipListItems.Controls.Add(mnuListItemCurrentView);
mnuZipListItems.Controls.Add(mnuListItemCurrentViewVersions);
this.Controls.Add(mnuZipListItems);
}
}
在回发处理程序中,简单地检索列表项文件,将它们压缩到一个临时目录,并强制浏览器下载该文件:
void PushFileToDownload(string FilePath, string FileName)
{
FileInfo fInfo = new FileInfo(FilePath);
HttpContext.Current.Response.ContentType = "application/x-download";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
HttpContext.Current.Response.AddHeader("Content-Length", fInfo.Length.ToString());
HttpContext.Current.Response.WriteFile(FilePath);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}