SharePoint 2010 ——自定义上传页面与多文件上传解决方案
最近项目遇到一个很麻烦的问题,原以为很容易解决,结果搞了那么久,先开个头,再慢慢写
SharePoint 2010 ——自定义上传页面与多文件上传解决方案
1.创建Sharepoint空白项目,创建应用程序页面,创建custom action,
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Description="Customize the upload.aspx file for a document library"
RegistrationType="List"
RegistrationId="101"
GroupId="Permissions"
Id="05511583-fb44-4eca-817a-45892250da9e"
Location="Microsoft.SharePoint.ListEdit"
Sequence="1000"
Title="Customize Upload Form (TCL SUNJUNLIN)"
>
<UrlAction Url="~site/_layouts/Custom.ApplicationPage/SetCustomUploadProperties.aspx?List={ListId}" />
</CustomAction>
</Elements>
2自定义项目feature.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration; namespace Custom.ApplicationPage.Features.CustomApp
{
/// <summary>
/// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
/// </summary>
/// <remarks>
/// 附加到此类的 GUID 可能会在打包期间使用,不应进行修改。
/// </remarks> [Guid("a4314576-6717-47fc-909b-8e692f1d32c1")]
public class CustomAppEventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.CustomUploadPage = "/_layouts/Custom.ApplicationPage/CustomUpload.aspx";
web.Update();
}
catch (Exception ex)
{
LogException(ex);
throw;
}
} public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.CustomUploadPage = "";
web.Update();
}
catch (Exception ex)
{
LogException(ex);
throw;
}
}
public static void LogException(Exception ex)
{
if (ex.InnerException != null)
LogException(ex.InnerException);
Log(ex.Message, ex.StackTrace);
}
public static void Log(string Message, string StackTrace)
{
//log to ULS
SPDiagnosticsService.Local.WriteTrace(, new SPDiagnosticsCategory("TCL.CustomUpload", TraceSeverity.High, EventSeverity.ErrorCritical), TraceSeverity.Unexpected, Message, StackTrace);
}
}
}
3.编写 自定义 上传页面;
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomUpload.aspx.cs" Inherits="Custom.ApplicationPage.Layouts.CustomUpload"
DynamicMasterPageFile="~masterurl/default.master" %> <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<link href="/_layouts/Custom.ApplicationPage/JS/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/_layouts/Custom.ApplicationPage/JS/jquery-1.9.1.min.js" ></script>
<script type="text/javascript" src="/_layouts/Custom.ApplicationPage/JS/jquery.uploadify.js"></script> <script type="text/javascript">
$(document).ready(function () {
var filelist = "";
var currentDocLib = $("#<%=hiddenCurrentDocLib.ClientID %>").val();
var listID = "";
if (currentDocLib != "" && currentDocLib != null) { listID = currentDocLib.substring(currentDocLib.indexOf('{') + , currentDocLib.lastIndexOf('}'));
}
$("#uploadify").uploadify({
'swf': '/_layouts/Custom.ApplicationPage/JS/uploadify.swf',
'uploader': '/_layouts/Custom.ApplicationPage/FilesHandler.ashx?listID=' + listID,
'cancelImg': '/_layouts/Custom.ApplicationPage/JS/uploadify-cancel.png',
'buttonText': '选择文件...',
'auto': false,
'multi': true,
'onSelect': function (file) { var mycars = new Array()
mycars[] = "\\"
mycars[] = "/"
mycars[] = ":"
mycars[] = "*"
mycars[] = "?"
mycars[] = "\""
mycars[] = "<"
mycars[] = ">"
mycars[] = "|"
mycars[] = "#"
mycars[] = "{"
mycars[] = "}"
mycars[] = "%"
mycars[] = "~"
mycars[] = "&" for (i = ; i < mycars.length; i++) {
var name = file.name;
if (name.indexOf(mycars[i]) != - && name != null) {
filelist = filelist + " \n " + name;
}
}
$("#<%=hidNotAllowFile.ClientID %>").val(filelist);
},
'onQueueComplete': function (queueData) { window.frameElement.commitPopup();
},
'onDialogClose': function (queueData) {
var notAllowFile = $("#<%=hidNotAllowFile.ClientID %>").val();
if (notAllowFile != "" && notAllowFile != null) {
alert(notAllowFile + '\n文件名包含非法字符,系统将自动替换为合法字符'); }
filelist = "";
} });
});
</script>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server" >
<div style="width:100%; height:300px;">
<div id="fileQueue">
</div>
<input type="file" name="uploadify" id="uploadify" />
<p style="text-align:right">
<a href="javascript:$('#uploadify').uploadify('upload', '*')">上传</a>
<a href="javascript:$('#uploadify').uploadify('cancel', '*')">取消</a>
</p> <asp:HiddenField ID="hidNotAllowFile" runat="server" /> <asp:HiddenField ID="hiddenCurrentDocLib" runat="server" />
</div>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
多文件上传
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
runat="server">
我的应用程序页上传文档
</asp:Content>
4.应用Uploadfiy上传插件
5.编写上传服务FilesHandler.ashx,这个文件需要手动创建,找不到这个模板,
参考:http://cn.bing.com/search?q=SharePoint%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%88%9B%E5%BB%BAHttpHandler+.&form=IE10TR&src=IE10TR&pc=LNJB
using System.Web;
using System.Runtime.InteropServices;
using System;
using System.IO;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections; namespace Custom.ApplicationPage.Layouts.Custom.ApplicationPage
{
[Guid("26c725b1-07d3-4326-ab6b-7343abe0a7ed")]
public class FilesHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
string listID = "";
if (context.Request.QueryString["listID"] != null && !string.IsNullOrEmpty(context.Request.QueryString["listID"].ToString()))
{
listID = context.Request.QueryString["listID"]; } HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath =
HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
//file.SaveAs(uploadPath + file.FileName);
OnSumitToMOSS(file, context, listID);
//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
context.Response.Write("");
}
else
{
context.Response.Write("");
}
}
public void OnSumitToMOSS(HttpPostedFile postedFile,HttpContext context,string listID)
{
try
{
using (SPSite site = SPContext.Current.Site)
{ using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;//关闭页面安全性验证
Guid gid=new Guid(listID);
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists[gid];
//SPDocumentLibrary docLib = (SPDocumentLibrary)web.GetList("http://moss:9527/DocLib");
///'检查文件扩展名字
if (postedFile != null)
{
string fileName = postedFile.FileName;
if (!string.IsNullOrEmpty(fileName))
{ ArrayList myChar = new ArrayList();
myChar.Add("\\");
myChar.Add( "/");
myChar.Add( ":");
myChar.Add( "*");
myChar.Add( "?");
myChar.Add( "\"");
myChar.Add( "<");
myChar.Add( ">");
myChar.Add( "|");
myChar.Add( "#");
myChar.Add( "{");
myChar.Add( "}");
myChar.Add( "%");
myChar.Add( "~");
myChar.Add( "&"); foreach (string str in myChar)
{
if (fileName.IndexOf(str) != -)
{
fileName= fileName.Replace(str, "_");
}
} SPFile file = docLib.RootFolder.Files.Add(fileName, postedFile.InputStream);//向文档库根目录添加文件
file.Update();//保存文件
docLib.Update();
web.Update();
}
}
web.AllowUnsafeUpdates = false;//上传完毕重新开启页面安全性验证
//this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>"); }
}
}
catch (System.Exception Ex)
{
context.Response.Write("<script type='text/javascript'>alert(" + Ex.Message + ");</script>");
}
} }
}
SharePoint 2010 ——自定义上传页面与多文件上传解决方案的更多相关文章
- PHP实现单文件、多文件上传 封装 面向对象实现文件上传
文件上传配置 客户端配置 1.表单页面 2.表单的发送方式为post 3.添加enctype = "multipart/form-data" <form action=&qu ...
- SpringBoot - 实现文件上传2(多文件上传、常用上传参数配置)
在前文中我介绍了 Spring Boot 项目如何实现单文件上传,而多文件上传逻辑和单文件上传基本一致,下面通过样例进行演示. 多文件上传 1,代码编写 1)首先在 static 目录中创建一个 up ...
- 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽)
首先需要导入一些js和css文件 ? 1 2 3 4 5 6 <link href="__PUBLIC__/CSS/bootstrap.css" rel="exte ...
- sharepoint 2010 自定义页面布局
在sharepoint开发中经常遇到 自定义网站栏.内容类型,页面布局和模板页也会遇到,遇到机会就相对比较小. 首先新建一个空的sharepoint项目: 1)创建网站兰: 修改SiteColumns ...
- sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式
上个月本人开发和上线了一个在SharePoint 2010上基于HTML5的移动OA网站,后端服务采用自定义的基于AgilePoint工作流引擎的Sharepoint Web服务,前端主要采用Jque ...
- [SharePoint 2010] 自定义字段类型开发(二)
在SharePoint 2010中实现View Action Button效果. http://www.sharepointblogs.be/blogs/vandest/archive/2008/06 ...
- 自定义MVC框架之工具类-文件上传类
截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...
- SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用
通常在我们的项目中,都会涉及到母版页的定制.并且必不可少的,需要配合以一套自己的JavaScript框架和CSS样式.你有没有遇到过这样的情况呢,在开发环境和UAT时都还算顺利,但是当最终部署到生产服 ...
- 让Android中的webview支持页面中的文件上传
android webview在默认情况下是不支持网页中的文件上传功能的: 如果在网页中有<input type="file" />,在android webview中 ...
随机推荐
- eclipse 手动/自动安装插件
只要你的Eclipse的压缩包,一般为xxx.zip,其内部包含了对应的features和plugins文件夹,(不管是否还有content.jar和artifacts.jar)则都可以: 要么手动解 ...
- Robot Framework自动化测试(四)--- 分层思想
谈到Robot Framework 分层的思想,就不得不提“关键字驱动”. 关键字驱动: 通过调用的关键字不同,从而引起测试结果的不同. 在上一节的selenium API 中所介绍的方法其实就是关 ...
- Love
愿这段代码陪我走过此生,献给我最爱的榨菜. /** *@Description:<p>我爱榨菜</p> *@author 王旭 *@time 2016年4月25日 下午7:58 ...
- 0414-复利计算器6.0.Release
复利计算器6.0--Release 前言 本次复利计算器的版本更新,主要有以下内容的完善: 1.优化了Web版的页面,提供了更舒服美观的用户体现. 2.新增了移动端(安卓)app版本. 版本信息 项目 ...
- 使用SQLite数据库和Access数据库的一些经验总结
在我的<Winform开发框架>中,可使用多种数据库作为程序的数据源,除了常规的Oracle数据库.SqlServer.MySql数据库,其中还包括了SQLite数据库.Access数据库 ...
- 安装、部署... Windows服务 .net程序 安装 命令
@echo offInstallutil.exe 程序目录 F:\test\TestWindows.exe 服务程序目录@sc start "服务名称"@sc config &qu ...
- Ext.NET 4.1.0 搭建页面布局
Ext.NET目前的最新版本为4.1.0,可以从官网:ext.net上下载,具体下载网址为:http://ext.net/download/. 文件下载下来后,在\lib\目录下存在3个文件夹,分别对 ...
- VC Windows API获得桌面所有窗口句柄的方法
VC Windows API应用之GetDesktopWindow ——获得桌面所有窗口句柄的方法 Windows API Windows 这个多作业系统除了协调应用程序的执行.分配内存.管理资源…之 ...
- MAC OS X 系统怎么样?
朝鲜的 IT 应用状况并不为外界所熟知,过去媒体纷纷报道,朝鲜已故领导人金正日酷爱苹果电子产品,而最近一份调查报告显示,在朝鲜个人电脑操作系统市场,苹果 MAC OS X 系统位居第一名,遥遥领先微软 ...
- 安装win8、ubuntu双系统的过程
弄了一个晚上,终于完成了,之前是用虚拟机的,但是觉得不带劲,并且折腾来时菜鸟变大神的捷径,虽然现在还一直在爬坑.继续奋斗吧...王小二 首先是看 ubuntu 百度贴吧的安装帖子(http://tie ...