using (SPSite site = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb web = site.OpenWeb()) { SPSecurity.RunWithElevatedPrivileges(delegate() { SPDocumentLibrary documentLib = web.Lists["Shared Documents"] as SPDocumentLibrary; Stream fStream = FileUpload1.PostedFile.InputStream; byte[] _byteArray = new byte[fStream.Length]; fStream.Read(_byteArray, 0, (int)fStream.Length); fStream.Close(); web.AllowUnsafeUpdates = true; string _fileUrl = documentLib.RootFolder.Url + "/" + FileUpload1.PostedFile.FileName; bool IsOverwriteFile = true; SPFile file = documentLib.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile); SPListItem item = file.Item; item["Title"] = FileUpload1.PostedFile.FileName; item.Update(); file.Update(); web.AllowUnsafeUpdates = false; }); } } - See more at: http://sharepointdotnetcart.blogspot.ca/2013/11/programmatically-upload-file-to.html#sthash.EnPM1XzB.dpuf
No comments:
Post a Comment