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);
} }
}