在SharePoint事件接收器中下载文档 [英] Download document in SharePoint event receiver

查看:84
本文介绍了在SharePoint事件接收器中下载文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


你能指导我吗?我想在文档库中编写一个事件接收器。上传文件后应立即触发事件。我需要为其他一些活动下载上传的文件。


我怎么能这样做?


谢谢&问候


Poomani Sankaran

解决方案

Hi Poomani,


<我们可以为文档库创建一个事件接收器,然后在ItemAdded方法中将上传文档下载到服务器中的文件夹中。以下代码供您参考。

以下代码供您参考。 
public override void ItemAdded(SPItemEventProperties properties)
{
if(properties.ListItem.File.Length == 0)
{
System.Threading.Thread.Sleep (2000);

//由于我们的项存在,运行GetItemById来实例化一个新的和更新的SPListItem对象
var spFile = properties.List.GetItemById(properties.ListItemId);
while(spFile.File.LockType!= SPFile.SPLockType.None)
{
System.Threading.Thread.Sleep(2000);
spFile = properties.List.GetItemById(properties.ListItemId);
//我们需要检查文件是否存在,否则如果有人决定取消上传
if(!spFile.File.Exists)
return,它将永远循环;
}
SPFile file = spFile.File;
byte [] bArray = file.OpenBinary();
string filePath = Path.Combine(" c:\\ temp \\ download",file.Name);
//打开文件流并使用(FileStream fs = new FileStream(filePath,FileMode.Create,FileAccess.ReadWrite))写入
文件
{
fs.Write( bArray,0,bArray.Length);
}
}
else
{
SPFile file = properties.ListItem.File;
byte [] bArray = file.OpenBinary();
string filePath = Path.Combine(" c:\\ temp \\ download",file.Name);
//打开文件流并使用(FileStream fs = new FileStream(filePath,FileMode.Create,FileAccess.ReadWrite))写入
文件
{
fs.Write( bArray,0,bArray.Length);
}

}
}


最诚挚的问候,


丹尼斯


Hi All,

Could you please guide me? I want to write a event receiver in document library. Event should fire immediate after upload document. I need to download the uploaded document for some other activities.

How could i do it?

Thanks & Regards

Poomani Sankaran

解决方案

Hi Poomani,

We can create an event receiver for document library, and then download the upload document to a folder in server in ItemAdded method. The following code for your reference.

The following code for your reference.
public override void ItemAdded(SPItemEventProperties properties)
{
	if (properties.ListItem.File.Length == 0)
	{
		System.Threading.Thread.Sleep(2000);

		//Since our item exists, run the GetItemById to instantiate a new  and updated SPListItem object 
		var spFile = properties.List.GetItemById(properties.ListItemId);
		while (spFile.File.LockType != SPFile.SPLockType.None)
		{
			System.Threading.Thread.Sleep(2000);
			spFile = properties.List.GetItemById(properties.ListItemId);
			//We need to check if the file exists, otherwise it will loop forever if someone decides to cancel the upload
			if (!spFile.File.Exists)
				return;
		}
		SPFile file = spFile.File;
		byte[] bArray = file.OpenBinary();
		string filePath = Path.Combine("c:\\temp\\download", file.Name);
		//open the file stream and write the file
		using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
		{
			fs.Write(bArray, 0, bArray.Length);
		}
	}
	else
	{
		SPFile file = properties.ListItem.File;
		byte[] bArray = file.OpenBinary();
		string filePath = Path.Combine("c:\\temp\\download", file.Name);
		//open the file stream and write the file
		using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
		{
			fs.Write(bArray, 0, bArray.Length);
		}
		
	}
}

Best Regards,

Dennis


这篇关于在SharePoint事件接收器中下载文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆