using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Web;
using System.IO; namespace TestSharePointProject.DocEvent
{
/// <summary>
/// List Item Events
/// </summary>
public class DocEvent : SPItemEventReceiver
{
HttpContext currentContext;
public DocEvent()
{
currentContext = HttpContext.Current;
} /// <summary>
/// An item is being added.
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
//将上载的文件URL按.分为两部分:URL和文件类型
string[] fileUrl = properties.BeforeUrl.Split('.');
if (fileUrl.Length == )
{
properties.ErrorMessage = "文件类型不合法!";
properties.Cancel = true;
}
else
{
//获取上载的文件类型
string postFix = fileUrl[fileUrl.Length - ].ToLower();
if (!postFix.Contains("xlsx") && !postFix.Contains("xls"))
{
properties.ErrorMessage = "请选择上传Excel文件!";
properties.Cancel = true;
}
else {
if (currentContext != null)
{
if (currentContext.Request.Files.Count > )
{
//获取Item中被上传的所有文件
for (int i = ; i < currentContext.Request.Files.Count; i++)
{
if (!string.IsNullOrEmpty(currentContext.Request.Files[i].FileName))
{
FileStream file = null;
string path = currentContext.Request.Files[i].FileName.ToString(); // Read the file. This appears to be the offending line
file = File.OpenRead(path);
byte[] Content = new byte[file.Length];
file.Read(Content, , (int)file.Length);
file.Close();
file.Dispose();
//itemToAdd.Attachments.Add(currentContext.Request.Files[i].FileName, Content);
}
}
}
}
}
}
} /// <summary>
/// An item is being updated.
/// </summary>
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
} }
}

Sharepoint 列表ItemAdding事件判断文件类型、获取当前上传的文件的更多相关文章

  1. jmert中如何测试上传文件接口(测试上传excel文件)

    第一次用jmeter这个工具测试上传接口,以前没做过这一块,导致走了很多弯路.特地把经验谢谢,怕自己以后忘记... 一,jmeter如何上传文件 jmeter 的 http requests post ...

  2. JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。

    JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...

  3. web.config文件配置解决网站上传大文件限制

    Asp.Net网站对上传文件的大小,请求执行的时间都做了限制,上传的文件如果超过限制或者执行上传时间超出, 文件上传都将失败. 因此,需要配置web.config来增加最大文件上传的大小和执行超时时间 ...

  4. 上传文件到CDN,上传后文件错误。

    开始用xfp上传,发现文件错误.后来改用filezilla,上传后文件正确.害苦了我啊

  5. C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据

    转载:http://www.cnblogs.com/kivenhou/archive/2013/02/22/2921954.html 操作List前请设置SPWeb的allowUnsafeUpdate ...

  6. Github使用.gitignore文件忽略不必要上传的文件 (转)

    原文地址: https://blog.csdn.net/gjy211/article/details/51607347 常用编程语言及各种框架平台下的通用   .gitignore   文件 http ...

  7. python发送post请求上传文件,无法解析上传的文件

    前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...

  8. div滚动条弹出层效果 (所需要的css文件和js文件,都已经上传到文件里面了progressbar.rar)

    <%--总的弹出层--%> <div class="tcck" id="joinclub" style="display:none& ...

  9. plupload上传整个文件夹

    大容量文件上传早已不是什么新鲜问题,在.net 2.0时代,HTML5也还没有问世,要实现这样的功能,要么是改web.config,要么是用flash,要么是用一些第三方控件,然而这些解决问题的方法要 ...

随机推荐

  1. Hybrid App简介

    Hybrid App 是混合模式应用的简称,兼具 Native App 和 Web App 两种模式应用的优势,开发成本低,拥有Web技术跨平台特性.目前大家所知道的基于中间件的移动开发框架都是采用的 ...

  2. Windows Azure 虚拟网络配置(Point to Site)

    说明:本文以Azure国际版为例,中国版在网络位置会存在一定差异. 1. 场景 虚拟网络为我们提供了在Windows Azure云计算环境上构建网络定义的能力,通过虚拟网络,我们可以方便地将Windo ...

  3. ERROR (ClientException) nova image-list

    nova image-listERROR (ClientException): The server has either erred or is incapable of performi9e-6c ...

  4. component to string

    component to string string to component ObjectTextToBinary ObjectBinaryToText ReadComponent #include ...

  5. 转】mysql数据库delete数据时不支持表别名

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4012853.html 感谢! 今天在帮同事查看一条删除的SQL语句执行出错的问题 SQL语句如下: 1 DELE ...

  6. C++ __int64用法(转)

    在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647.而unsig ...

  7. C++11lambda表达式

    [C++11lambda表达式] mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行. 使用示例: #include <vector> #include ...

  8. HDU 3923 Invoker(polya定理+逆元)

    Invoker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)Total Su ...

  9. POJ 1696 Space Ant(极角排序)

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2489   Accepted: 1567 Descrip ...

  10. Windows 7 不同安装模式简要区别(图解)

    ★ 你可能对GHOST不支持AHCI感到迷惑,实际上,写过GHOST一键安装批处理的都知道一个叫FINDCD.EXE的小程序,可是这个程序老 了,AHCI模式光驱他找不到了,找不到光驱动意味着光盘中G ...