SharePoint 2010文档库批量下载文档的实现

在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除、批量签出、批量签入等,但是,很遗憾,没有提供批量下载,如图:

若选中多个文档后,会发现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[i])).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 

复制代码

 

 

 

运行效果:

 

选中文档,点击“Multiple Downloads”后,直接弹出IE的下载对话框:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439231.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【NC54 三数之和】(待整理)

描述 给出一个有n个元素的数组S&#xff0c;S中是否有元素a,b,c满足abc0&#xff1f;找出数组S中所有满足条件的三元组。 数据范围&#xff1a;0 \le n \le 10000≤n≤1000&#xff0c;数组中各个元素值满足 |val | \le 100∣val∣≤100 空间复杂度&#xff1a;O(n^2)O(n2)&a…

【转】C#执行rar,zip文件压缩的几种方法及我遇到的坑总结

工作项目中需要用到zip压缩解压缩文件&#xff0c;一开始看上了Ionic.Zip.dll这个类库&#xff0c;操作方便&#xff0c;写法简单 对应有个ziphelper类 using Ionic.Zip;public static class ZipHelper{public static void UnZip(string zipPath, string outPath){try{using (…

【LeetCode - 42. 接雨水】

42. 接雨水 难度困难3164 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 示例 1&#xff1a; 输入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 输出&#xff1a;6 解释&#xff1a;上面是由数组 […

【转】C#打包文件夹成zip格式(包括文件夹和子文件夹下的所有文件)

C#打包zip文件可以调用现成的第三方dll&#xff0c;事半功倍&#xff0c;而且该dll完全免费&#xff0c;下载地址&#xff1a;SharpZipLib 下载完解压缩后&#xff0c;把 ICSharpCode.SharpZipLib.dll 拷贝到当前项目的目录下&#xff08;如果偷懒的话&#xff0c;可以直接拷贝…

【LeetCode】第283场周赛题解

本场题题目不难&#xff0c;但是力求写出精简优雅的代码&#xff0c;还是有需要学习的地方的。 第一题 力扣 class Solution:def cellsInRange(self, s: str) -> List[str]:ans []a,b,c,d s[0],s[1],s[3],s[4]for i in range(ord(a), ord(c)1):for j in range(int(b),int…

Linq to Sql : 三种事务处理方式

Linq to SQL支持三种事务处理模型&#xff1a;显式本地事务、显式可分发事务、隐式事务。(from MSDN: 事务 (LINQ to SQL))。MSDN中描述得相对比较粗狂&#xff0c;下面就结合实例来对此进行阐述。 0. 测试环境 OSWindows Server 2008 Enterprise sp1IDEVisual Studio 2008, …

【LeetCode - 33】搜索旋转排序数组(二分)

力扣 解题报告&#xff1a; 二分。但是有不少细节要考虑清楚。 所以干脆考虑另一种二分的方式。也就是第二次二分的时候&#xff0c;把两半数组给拼成一个完整的数组&#xff0c;当然下标需要是虚拟的&#xff0c;这一步可以用偏移量取模完成。这样就不需要考虑边界情况了。 …

SHAREPOINT - CAML列表查询

首先要了解的是CAML(Collaboration Application Markup Language)不仅仅是用在对列表、文档库的查询&#xff0c;字段的定义&#xff0c;站点定义等处处使用的都是CAML。 简单的提一下CAML列表查询相关知识&#xff0c;请注意CAML查询无论对于标签还是值均大小写敏感 CAML查询…

【LeetCode - 1765】. 地图中的最高点

力扣 解题报告&#xff1a; 多元BFS。 进阶一下&#xff1a; 二维数组&#xff0c;1表示等高线&#xff0c;0表示平地&#xff0c;比如 输入 010 111 010 输出 010 121 010输入 010 101 010 输出 010 111 010即输入一个二维地图&#xff0c;保证等高线一定是闭合的环&#x…

Linq找不到行或行已更改

1.debug确认&#xff0c;待修改记录已经正确定位&#xff0c;各字段均已正确赋值 2.最后发现原来是Linq使用的表实际中有个字段&#xff0c;数据库中为Nullable&#xff0c;而dbml是Not Nullable&#xff0c;二者不一致 一个逻辑是&#xff0c;我现在填写的是非空值&#xff…

【LeetCode - 32】最长有效括号

给你一个只包含 ( 和 ) 的字符串&#xff0c;找出最长有效&#xff08;格式正确且连续&#xff09;括号子串的长度。 示例 1&#xff1a; 输入&#xff1a;s "(()" 输出&#xff1a;2 解释&#xff1a;最长有效括号子串是 "()" 示例 2&#xff1a; 输入…

【转】微服务架构下分布式事务方案

1 微服务的发展 微服务倡导将复杂的单体应用拆分为若干个功能简单、松耦合的服务&#xff0c;这样可以降低开发难度、增强扩展性、便于敏捷开发。当前被越来越多的开发者推崇&#xff0c;很多互联网行业巨头、开源社区等都开始了微服务的讨论和实践。Hailo有160个不同服务构成…

【LeetCode - 443】压缩字符串(模拟)

解题报告&#xff1a; 直接模拟。 class Solution { public:int compress(vector<char>& chars) {int p 0;for(int i 0; i<chars.size();) {int j i1;while(j<chars.size() && chars[j] chars[i]) j;chars[p] chars[i];if(j-i > 1) {int cnt…

Linq to SQL之使用事务

事务是一个原子的工作单位&#xff0c;必须完整的完成单位里的所有工作&#xff0c;要么全部执行&#xff0c;要么全部都不执行。如果提交事务&#xff0c;则事务执行成功&#xff1b;如果回滚事务&#xff0c;则事务执行失败。 事务具备4个基本特性--ACID(原子性、一致性、孤立…

【LeetCode - 798】得分最高的最小轮调(转化法)

解题报告&#xff1a; 思路一&#xff1a;这题首先说一个nlogn的方法。 首先一个主客转化&#xff0c;题目描述是说把数组做翻转&#xff0c;idx不变&#xff0c;然后nums[i]和i作比较。那么我们可以转化为让数组不变&#xff0c;idx转变&#xff0c;即&#xff1a;假设刚开始…

【转】聊聊分布式事务,再说说解决方案

前言 最近很久没有写博客了&#xff0c;一方面是因为公司事情最近比较忙&#xff0c;另外一方面是因为在进行 CAP 的下一阶段的开发工作&#xff0c;不过目前已经告一段落了。 接下来还是开始我们今天的话题&#xff0c;说说分布式事务&#xff0c;或者说是我眼中的分布式事务…

【LeetCode - 2049】统计最高分的节点数目

解题报告&#xff1b; 直接dp。注意mx也得longlong AC代码&#xff1a; class Solution { public:vector<int> vv[200005];int sum[200005];long long ans[200005];int n;void dfs(int x) {ans[x] 1; sum[x] 1;for(int i 0; i<vv[x].size(); i) {dfs(vv[x][i]);s…

Web Service 实现分布式事务

首先要声明&#xff0c;不推荐在web service中实现分布式事务。 原因如下&#xff1a;   1、webservice在通信层上是一种无连接的协议&#xff0c;每两次调用之间&#xff0c;tcp连接是断开的。而分布式事务需要保存事务上下文&#xff0c;这是一个难点   2、web service是…

【LeetCode每日一题】2024. 考试的最大困扰度

​​​​​​力扣 解题报告&#xff1a; 因为只有T和F两个元素&#xff0c;不难证明单向性。尺取法解决。当然这题也可以二分。 AC代码&#xff1a; class Solution { public:int maxConsecutiveAnswers(string answerKey, int k) {int l 0, r 0;int T 0, F 0;int ans …

使用WebService如何实现分布式事务

在 System.EnterpriseServices 名称空间中实现了COM服务的相关类&#xff0c;其中就提供事务支持。 你可以在你的方法上加上TransactionAttribute特性标记&#xff0c;那个方法就支持事务了。 然后在那个方法中就可以用ContextUtil.SetComplete()和ContextUtil.SetAbort()方法分…