设为首页 收藏本站
查看: 1063|回复: 0

[经验分享] SharePoint 2010文档库批量下载文档的实现

[复制链接]

尚未签到

发表于 2015-9-25 11:23:55 | 显示全部楼层 |阅读模式
  在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除、批量签出、批量签入等,但是,很遗憾,没有提供批量下载,如图:
   DSC0000.jpg
  若选中多个文档后,会发现Download a Copy这个Ribbon按钮变灰了,这几天,我自己做了一个Ribbon,实现了批量下载的功能,向大家介绍一下。
  先来说一下我这个批量下载的原理:
  1.       Ribbon按钮。在前端有一个Ribbon按钮,我也把它安置在Copies这个group里,它的作用是取得当前的文档库ID和所有被选中的条目的ID,作为参数传给下载页,下载页是一个Application Page。
  2.       获取文档库中的文档。在下载页完成,根据传递来的文档库ID和item ID, 获得对应的SPDocumentLibrary和SPFile。
  3.       把存放在数据库中的文档转化为实际的文档。在下载页完成,由于文档库中的文档是以二进制存放在数据库中,因此需要转化为实际的文档,为了打包方便,在服务器创建一个单独的文件夹存放,我以文档库的名字+当前的时间来作为文件夹的名称。
  4.       打包下载。在下载页完成,将对应的文件夹打包成.zip包,完成下载。
  要用到的技术:
  1.       自定义Ribbon。请参阅我的另一篇随笔:SharePoint 2010自定义开发Ribbon
  2.       Application Page。不再赘述。
  3.       压缩。我使用的开源的ICSharpCode.SharpZIPLib。
  开发工具还是使用Visual Studio 2010:
  通过Visual Studio 2010,可以非常方便的开发自定义Ribbon和Application Page。
  分别介绍一下:
  1.       Ribbon。
  主要来看一下这个Ribbon按钮的Command Action:


1 var ids='',url='';
2 var c=ctx.dictSel;
3 for (var key in c)
4 {
5 ids=ids+c[key].id+',';
6 };
7 if(ids!='')
8 {
9 url=ctx.HttpRoot+'/_layouts/downloads/download.aspx?listid='+ctx.listName+';'+ids;
10 window.open(url);
11 }
12
    
  其中,ctx为current context,类似于我们在后台使用SPContext,在SharePoint 2010页面中都会有这个context,它是一个ContextInfo对象,在一个页面的源文件中,可以看到
  


<script type="text/javascript">
ctx = new ContextInfo();              
var existingHash = '';      
if(window.location.href.indexOf("#") > -1)
{         existingHash = window.location.href.substr(window.location.href.indexOf("#"));      
}      
ctx.existingServerFilterHash = existingHash;      
if (ctx.existingServerFilterHash.indexOf("ServerFilter=") == 1)
{         ctx.existingServerFilterHash = ctx.existingServerFilterHash.replace(/-/g, '&').replace(/&&/g, '-');         var serverFilterRootFolder = GetUrlKeyValue("RootFolder", true,ctx.existingServerFilterHash);         var currentRootFolder = GetUrlKeyValue("RootFolder", true);        
if("" == serverFilterRootFolder && "" != currentRootFolder)        
{           ctx.existingServerFilterHash += "&RootFolder=" + currentRootFolder;         }         window.location.hash = '';         window.location.search = '?' + ctx.existingServerFilterHash.substr("ServerFilter=".length + 1);       }                  
ctx.listBaseType = 1;            
ctx.NavigateForFormsPages = false;      
ctx.listTemplate = "101";      
ctx.listName = "{E1616EE0-C898-435C-BFA8-CBC1C5D86B67}";      
ctx.view = "{FCBEAC6C-FAC6-4951-A06B-9561E7C8E8EC}";      
ctx.listUrlDir = "/Shared%20Documents";      
ctx.HttpPath = "http://TestSite:8080/_vti_bin/owssvr.dll?CS=65001";      
ctx.HttpRoot = "http://TestSite:8080";      
ctx.imagesPath = "/_layouts/images/";       ctx.PortalUrl = "";       ctx.SendToLocationName = "";       ctx.SendToLocationUrl = "";                  ctx.RecycleBinEnabled = 1;                ctx.OfficialFileName = "";       ctx.OfficialFileNames = "";       ctx.WriteSecurity = "1";       ctx.SiteTitle = "KevinTest";       ctx.ListTitle = "Shared Documents";       if (ctx.PortalUrl == "") ctx.PortalUrl = null;       ctx.displayFormUrl = "http://TestSite:8080/_layouts/listform.aspx?PageType=4&ListId={E1616EE0-C898-435C-BFA8-CBC1C5D86B67}";       ctx.editFormUrl = "http://TestSite:8080/_layouts/listform.aspx?PageType=6&ListId={E1616EE0-C898-435C-BFA8-CBC1C5D86B67}";       ctx.isWebEditorPreview = 0;       ctx.ctxId = 59;       ctx.isXslView = true;              if (g_ViewIdToViewCounterMap["{FCBEAC6C-FAC6-4951-A06B-9561E7C8E8EC}"] == null)           g_ViewIdToViewCounterMap["{FCBEAC6C-FAC6-4951-A06B-9561E7C8E8EC}"]= 59;       ctx.CurrentUserId = 1;                ctx.ContentTypesEnabled = true;              ctx59 = ctx;       g_ctxDict['ctx59'] = ctx;
</script>
  
  2.       下载页。
  很好理解,直接看代码吧。


  1 using System;
  2 using Microsoft.SharePoint;
  3 using Microsoft.SharePoint.WebControls;
  4 using System.Web;
  5 using System.IO;
  6 using System.Diagnostics;
  7
  8 using ICSharpCode.SharpZipLib.Zip;
  9 using ICSharpCode.SharpZipLib.Core;
10
11  
12 namespace ProjectFor8080.Layouts.Downloads
13 {
14     public partial class Download : LayoutsPageBase
15     {
16         protected void Page_Load(object sender, EventArgs e)
17         {
18             if (!string.IsNullOrEmpty(Request.Params["listid"]))
19             {
20                 SPContext context = SPContext.Current;
21
22                 SPWeb web = context.Web;
23
24                 string folder =  @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\template\LAYOUTS\Downloads\Files\";
25
26                 string listid = Request.Params["listid"];
27                 string[] downloadParams=listid.Split(';');
28                 string[] fileIds = downloadParams[1].Split(',');
29                 
30                 Guid listGuid=new Guid(downloadParams[0]);
31
32                 SPDocumentLibrary sdl = web.Lists[listGuid] as SPDocumentLibrary;
33
34                 //create the files folder under Downloads\Files
35                 string time = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
36
37                 string folderPath = folder + sdl.Title + time;
38
39                 Directory.CreateDirectory(folderPath);
40                 
41                 //download the files from library
42                 for(int i=0;i<fileIds.Length-1;i++)
43                 {
44                     SPFile file = sdl.GetItemById(Int32.Parse(fileIds)).File;
45                     string path = folderPath+@"\"+ file.Name;
46                     FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
47                     byte[] fileByte = file.OpenBinary();
48                     fs.Write(fileByte, 0, fileByte.Length);
49                     fs.Flush();
50                     fs.Close();
51
52                 }
53
54                 //zip file
55
56                 string zipName = sdl.Title+time+".zip";
57                 string zipPath=folder+zipName;
58                 CreateZipFile(folderPath, zipPath);
59                 string downloadUrl = context.Site.Url + @"/_layouts/downloads/files/" + zipName;
60
61                 Response.Redirect(downloadUrl);               
62             }
63             else
64                 return;
65         }
66         private static void CreateZipFile(string filesPath, string zipFilePath)
67         {
68             try
69             {
70                 string[] filenames = Directory.GetFiles(filesPath);
71
72                 using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
73                 {
74                     s.SetLevel(9); // 压缩级别 0-9
75
76                     //s.Password = "123"; //Zip压缩文件密码
77
78                     byte[] buffer = new byte[4096]; //缓冲区大小
79
80                     foreach (string file in filenames)
81                     {
82                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
83
84                         entry.DateTime = DateTime.Now;
85
86                         s.PutNextEntry(entry);
87
88                         using (FileStream fs = File.OpenRead(file))
89                         {
90                             int sourceBytes;
91                             do
92                             {
93                                 sourceBytes = fs.Read(buffer, 0, buffer.Length);
94                                 s.Write(buffer, 0, sourceBytes);
95                             } while (sourceBytes > 0);
96                         }
97                     }
98                     s.Finish();
99                     s.Close();
100                 }
101             }
102             catch (Exception ex)
103             {
104                 HttpContext.Current.Response.Write(ex.Message);
105             }
106         }
107     }
108 }
109   
  
  
  运行效果:
   DSC0001.jpg
  
  选中文档,点击&#8220;Multiple Downloads&#8221;后,直接弹出IE的下载对话框:
DSC0002.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118624-1-1.html 上篇帖子: 关于Sharepoint ad用户管理(源代码) 下篇帖子: SharePoint 2010 自定义Timer job 问题总结(拒绝访问,Execute方法不执行,不及时更新)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表