SharePoint 2010 编程上传文档

protected void btnUpload_Click(object sender, EventArgs e)

        {

            Boolean FileOK = false;

            Boolean FileSaved = false;

            String accountName = SPContext.Current.Web.CurrentUser.LoginName;

            string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

            String FileExtension = string.Empty;



            SPWeb web = SPContext.Current.Web;



            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                    using (SPSite currentSite = new SPSite(web.Site.Url))

                    {             

                        using (SPWeb currentWeb = currentSite.OpenWeb())

                        {



                            if (Upload.HasFile)

                            {

                                    FileExtension = Path.GetExtension(Upload.FileName.ToString()).ToLower();

                                    String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };

                                    for (int i = 0; i < allowedExtensions.Length; i++)

                                    {

                                            if (FileExtension == allowedExtensions[i])

                                            {

                                                    FileOK = true;

                                                    break;

                                            }

                                    }

                                }



                            if (FileOK)

                            {

                                    try

                                    {



                                            currentWeb.AllowUnsafeUpdates = true;

                                            SPSecurity.CatchAccessDeniedException = false;



                                            SPList listePictureUpload = currentWeb.GetList(SPUtility.ConcatUrls(currentWeb.Url, "User Photos"));

                                            SPFile itemPictureUpload = currentWeb.Files.Add(listePictureUpload.RootFolder.Url + "/" + Upload.FileName.Remove(Upload.FileName.Length -                                 FileExtension.Length) + fileNameWithoutExtension + FileExtension, Upload.FileBytes, true);

                                            //SPFile itemPictureUpload = listePictureUpload.RootFolder.Files.Add(Upload.FileName.Remove(Upload.FileName.Length - FileExtension.Length) +                                 fileNameWithoutExtension + FileExtension, Upload.FileBytes, true);

                                            currentWeb.AllowUnsafeUpdates = false;

                                    }

                             }

                      }

                }

    

         });



}

 

你可能感兴趣的:(SharePoint)