SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.AllWebs[SPContext.Current.Web.ID])
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists.TryGetList("文档"); //考虑到中英文的影响,这个在英文状态下名称可能需要改变
if (list != null)
{
LogHelper.WriteLog(LogArea.SPSUI, LogLevelType.INFO, "资产处置-找到文档库");
SPFolder folder = list.RootFolder;
HttpFileCollection files = Request.Files;
if (files.Count > 0 && fileUpload.HasFile)
{
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string onlyName = System.IO.Path.GetFileName(file.FileName);
string urlOfFile = DateTime.Now.ToString("yyyyMMddhhmmss") + onlyName;
SPFile oFile = folder.Files.Add(urlOfFile, file.InputStream, true);
oFile.Update();
File fileModel = new File
{
Id = Guid.NewGuid(),
Name = onlyName,
Url = oFile.ServerRelativeUrl,
Created = DateTime.Now,
CreateBy = web.CurrentUser.Name
};
result.Add(fileModel);
}
}
}
web.AllowUnsafeUpdates = false;
}
}
});