[英]How to check size of library document file before uploaded with event receiver
I'm new in sharepoint environment, but i have a question I wanna Create An Event Receiver to check the size of file in library that's user will upload , because I want to reject sizes bigger than 1 MB. 我是Sharepoint环境的新手,但是我有一个问题想创建一个事件接收器来检查用户将要上传的库中文件的大小 ,因为我想拒绝大于1 MB的大小。
any help will be appreciated 任何帮助将不胜感激
What I have tried: I'm tried catch the attachment file to get it's size but I can't ! 我尝试过的方法: 我尝试捕获附件文件来获取附件的大小,但是我做不到!
public override void ItemAdding(SPItemEventProperties properties){
using (SPSite oSPsite = new SPSite("http://win-sp:1001/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
SPDocumentLibrary oLibrary = oSPWeb.Lists["myLibrary"] as SPDocumentLibrary;
if (oLibrary != null)
{
properties.BeforeProperties["Title"].ToString();
}
}
}
base.ItemAdding(properties);
}
You can try and modify the below mentioned code: 您可以尝试修改以下提到的代码:
We need to use either the properties.AfterProperties["vti_filesize"]
or properties.ListItem.File.TotalLength
to check the file size. 我们需要使用properties.AfterProperties["vti_filesize"]
或properties.ListItem.File.TotalLength
来检查文件大小。
public override void ItemAdding(SPItemEventProperties properties)
{
long validFileSize = 1000000;//1MB;
long currentFileSize;
if (properties.ListItem == null)
{
using (SPWeb web = properties.OpenWeb())
{
if (properties.ListTitle.ToLower() == "myLibrary")
{
currentFileSize = long.Parse(properties.AfterProperties["vti_filesize"].ToString());
if (currentFileSize > validFileSize)
{
return false;
}
}
}
}
else if (properties.ListItem.ParentList.Title.ToLower() == "myLibrary")
{
currentFileSize = properties.ListItem.File.TotalLength;
if (currentFileSize > validFileSize)
{
return false;
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.