public void AddNow(string leafName, Base64EncodedByteArrayInstance data) { if (data == null) { throw new JavaScriptException(this.Engine, "Error", "A Base64EncodedByteArrayInstance must be supplied as the attachment data to add."); } m_attachmentCollection.AddNow(leafName, data.Data); }
public static void AttachFile(this SPListItem item, Stream fileStream, string originalleafName, bool overwrite = false, bool updateRequired = false) { SPAttachmentCollection attachments = item.Attachments; SPList list = item.ParentList; SPWeb web = list.ParentWeb; if (!list.EnableAttachments) { //throw new SPException(string.Format("The specified file can not be added to the list \"{0}\" with disabled attachments.", list.Title)); return; } string leafName = SPHelper.RemoveIllegalUrlCharacters(originalleafName); if (fileStream == null || fileStream.Length == 0) { throw new SPException("The specified file should not be empty."); } var maximumFileSize = web.Site.WebApplication.MaximumFileSize * 1024 * 1024; if (fileStream.Length > maximumFileSize) { throw new SPException(string.Format("The specified file is larger than the maximum supported file size: {0}.", SPUtility.FormatSize(maximumFileSize))); } if (overwrite) { string attachmentUrl = SPUrlUtility.CombineUrl(attachments.UrlPrefix, leafName); SPFile existingFile = web.GetFile(attachmentUrl); if (existingFile.Exists) { existingFile.SaveBinary(fileStream); return; } } BinaryReader reader = new BinaryReader(fileStream); if (updateRequired) { attachments.Add(leafName, reader.ReadBytes((int)fileStream.Length)); } else { attachments.AddNow(leafName, reader.ReadBytes((int)fileStream.Length)); } reader.Close(); }