SharePoint2013 上传文件到文档库

时间:2021-12-30 08:56:16
 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;
}
}
});

 

相关文章