码迷,mamicode.com
首页 > Web开发 > 详细

SharePoint2013 上传文件到文档库

时间:2016-04-06 11:09:14      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 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;
                        }
                    }
                });

 

SharePoint2013 上传文件到文档库

标签:

原文地址:http://www.cnblogs.com/lishidefengchen/p/5358055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!