//将指定文件上传到指定的文件夹,上传文件需要考虑:文件大小限制、文件上传次数限制、文件同名限制、fileupload控件为空
protected void btnFileUpload_Click(object sender, EventArgs e)
{
//如果FileUpload控件为空,返回
if (this.fupLocalFile.FileName=="")
{
return;
}
if (IsOverTime())
{
Response.Write("<script language='javascript'>alert('您今天上传文件数已经达到指定个数,如有疑问请联系管理员!');</script>");
return;
}
SPList lstBBSLibrary = GetLibraryByRandom();
SPFolder folder = lstBBSLibrary.RootFolder;
string filePath = this.fupLocalFile.PostedFile.FileName;
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
if (IsFileExists(lstBBSLibrary, fileName))
{
if (GetNoFileLibrary(lstBBSLibrary.Title,fileName) == null)
{
Response.Write("<script language='javascript'>alert('已有相同名称的文件存在,请更改文件名后上传!');</script>");
return;
}
lstBBSLibrary = GetNoFileLibrary(lstBBSLibrary.Title,fileName);
}
int dataLen = this.fupLocalFile.PostedFile.ContentLength;
if (IsOverSize(dataLen / (1024 * 1024) + 1))
{
Response.Write("<script language='javascript'>alert('上传文件超过指定最大值,如有疑问请联系管理员!');</script>");
return;
}
Stream fileDataStream = this.fupLocalFile.PostedFile.InputStream;
string fileType = this.fupLocalFile.PostedFile.ContentType;
byte[] fileData = new byte[dataLen];
fileDataStream.Read(fileData, 0, dataLen);
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
folder.Files.Add(Path.GetFileName(this.fupLocalFile.PostedFile.FileName), fileData, false);
web.AllowUnsafeUpdates = false;
}
/// <summary>
/// 按照顺时针获得没有指定文件名的文档库
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private SPList GetNoFileLibrary(string currentLibraryName, string fileName)
{
int currentIndex = int.Parse(currentLibraryName.Substring(currentLibraryName.LastIndexOf('y')));
SPList lstNoFileLibrary = null;
for (int i = currentIndex + 1; i < 6; i++)
{
SPList lstBBSLibrary = SPContext.Current.Web.Lists["BBSLibrary" + i];
if (!IsFileExists(lstBBSLibrary, fileName))
{
lstNoFileLibrary = lstBBSLibrary;
break;
}
}
if (lstNoFileLibrary == null)
{
for (int i = 1; i < currentIndex + 1; i++)
{
SPList lstBBSLibrary = SPContext.Current.Web.Lists["BBSLibrary" + i];
if (!IsFileExists(lstBBSLibrary, fileName))
{
lstNoFileLibrary = lstBBSLibrary;
break;
}
}
}
return lstNoFileLibrary;
}
/// <summary>
/// 随机选取存储上传文件的Document Library
/// </summary>
/// <returns></returns>
public SPList GetLibraryByRandom()
{
Random ran = new Random();
int libNum = ran.Next(1, 5);
SPList lstBBSLibrary = SPContext.Current.Web.Lists["BBSLibrary" + libNum];
return lstBBSLibrary;
}
/// <summary>
/// 查询结果为空或者上传文件超过指定大小,返回true,否则返回false;
/// </summary>
/// <param name="localFileSize"></param>
/// <returns></returns>
public bool IsOverSize(int localFileSize)
{
SPListItem item = GetConfigureItem("FileMaxSize");
bool result = true;
//查询结果为空或者上传文件超过指定大小,返回true,否则返回false;
if (item != null && item["Num"] != null && int.Parse(item["Num"].ToString()) > localFileSize)
{
result = false;
}
return result;
}
/// <summary>
/// 检索5个BBSLibraries,取出createdby为currentuser,created为currentdate的文件记录
/// </summary>
/// <returns>查询结果为空或者小于指定个数则返回false,否则为true </returns>
public bool IsOverTime()
{
int totalNum = 0;
totalNum = GetUploadedFileNum("BBSLibrary1") + GetUploadedFileNum("BBSLibrary2")
+ GetUploadedFileNum("BBSLibrary3") + GetUploadedFileNum("BBSLibrary4") + GetUploadedFileNum("BBSLibrary5");
SPListItem item = GetConfigureItem("FileMaxUploadTimes");
bool result = false;
if (item != null && item["Num"] != null && totalNum > int.Parse(item["Num"].ToString()))
{
result = true;
}
return result;
}
/// <summary>
/// 获得当前用户当天在某个文档库中已上传文档数量
/// </summary>
/// <param name="lstBBSLibrary"></param>
/// <param name="currentUser"></param>
/// <param name="currentDate"></param>
/// <returns></returns>
public int GetUploadedFileNum(string lstBBSLibraryName)
{
//获得系统当天最早时间,并将其转化为sharepoint能够识别的时间
string currentDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.Date);
//获得当前用户
SPUser currentUser = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context).CurrentUser;
SPList lstBBSLibrary = SPContext.Current.Web.Lists[lstBBSLibraryName];
SPQuery query = new SPQuery();
query.Query = string.Format(@"
<Where>
<And>
<Eq>
<FieldRef Name='Author' />
<Value Type='User'>{0}</Value>
</Eq>
<Gt>
<FieldRef Name='Created' />
<Value Type='DateTime'>{1}</Value>
</Gt>
</And>
</Where>", currentUser, currentDate);
SPListItemCollection items = lstBBSLibrary.GetItems(query);
int result = 0;
if (items.Count > 0)
{
result = items.Count;
}
return result;
}
/// <summary>
/// 获得某个配置信息
/// </summary>
/// <returns></returns>
public SPListItem GetConfigureItem(string condition)
{
SPList lstBBSConfigure = SPContext.Current.Web.Lists[ListNames.BBSConfigure];
SPQuery query = new SPQuery();
query.Query = string.Format(@" <Where>
<Eq>
<FieldRef Name='Title' />
<Value Type='Text'>{0}</Value>
</Eq>
</Where>", condition);
query.ViewFields = string.Format(@"<FieldRef Name='Title' />
<FieldRef Name='Num' />");
query.RowLimit = 1;
SPListItemCollection items = lstBBSConfigure.GetItems(query);
SPListItem item = null;
if (items.Count > 0)
{
item = items[0];
}
return item;
}
/// <summary>
/// 判断文档库中是否已存在该文件
/// </summary>
/// <param name="lstLibrary"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public bool IsFileExists(SPList lstLibrary, string fileName)
{
SPQuery query = new SPQuery();
query.Query = string.Format(@"<Where>
<Eq>
<FieldRef Name='Title' />
<Value Type='Text'>{0}</Value>
</Eq>
</Where>", fileName);
query.ViewAttributes = "Scope=\"Recursive\"";
SPListItemCollection items = lstLibrary.GetItems(query);
bool result = true;
if (items.Count < 1)
{
result = false;
}
return result;
}