Esempio n. 1
0
        public void Add(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.Add(leafName, data.Data);
        }
Esempio n. 2
0
        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();
        }
 protected void btnOk_click(object sender, EventArgs e)
 {
     try
     {
         if (!string.IsNullOrEmpty(Request["ListId"]) && !string.IsNullOrEmpty(Request["ItemId"]))
         {
             Guid   listId = new Guid(Request["ListId"]);
             Int32  itemId = Convert.ToInt32(Request["ItemId"]);
             SPWeb  webObj = SPContext.Current.Web;
             SPList lstObj = webObj.Lists[listId];
             if (lstObj != null)
             {
                 SPListItem thisItem = lstObj.GetItemById(itemId);
                 if (thisItem != null)
                 {
                     if (fileUploadControl.HasFiles)
                     {
                         SPAttachmentCollection attach        = thisItem.Attachments;
                         HttpFileCollection     uploadedFiles = Request.Files;
                         for (int i = 0; i < uploadedFiles.Count; i++)
                         {
                             HttpPostedFile userPostedFile = uploadedFiles[i];
                             String         fileName       = userPostedFile.FileName;
                             using (Stream attachmentStream = userPostedFile.InputStream)
                             {
                                 Byte[] attachmentContent = new Byte[attachmentStream.Length];
                                 attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
                                 attach.Add(fileName, attachmentContent);
                             }
                         }
                         thisItem.Update();
                         Page.ClientScript.RegisterStartupScript(this.GetType(), "CallClosePopup", "closePopup(1);", true);
                     }
                     else
                     {
                         lblError.Text = "The file name is invalid or the file is empty. A file name cannot contain any of the following characters: \\ / : * ? \" < > | # { } % ~ &";
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         lblError.Text = ex.Message;
     }
 }
Esempio n. 4
0
        private void exportUtility_ExportCompleted(object sender, ExportCompletedEventArgs e)
        {
            GlymaExportUserState userState = e.UserState as GlymaExportUserState;

            if (userState != null)
            {
                SPWorkItem workItem = userState.StateObject as SPWorkItem;
                if (workItem != null)
                {
                    using (SPSite site = new SPSite(workItem.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(workItem.WebId))
                        {
                            try
                            {
                                if (e.Status == ExportStatus.Error)
                                {
                                    WriteExportStatus(workItem, ExportStatus.Error);
                                    LogMessage(workItem, e.ErrorMessage);
                                }
                                else if (e.Status == ExportStatus.Completed)
                                {
                                    SPList     exportsList = web.Lists[workItem.ParentId];
                                    SPListItem exportItem  = exportsList.GetItemByUniqueId(workItem.ItemGuid);

                                    if (exportItem != null)
                                    {
                                        // Read the contents of the file
                                        byte[] fileContents = null;
                                        Stream fs           = null;
                                        try
                                        {
                                            fs           = File.OpenRead(e.FileLocation);
                                            fileContents = new byte[fs.Length];
                                            fs.Read(fileContents, 0, (int)fs.Length);

                                            // Add the file to the ListItem
                                            SPAttachmentCollection attachments = exportItem.Attachments;
                                            string fileName  = Path.GetFileName(e.FileLocation);
                                            string extention = fileName.Substring(fileName.LastIndexOf('.'));
                                            fileName = e.MapName + extention;
                                            attachments.Add(fileName, fileContents);
                                            exportItem.Update();

                                            // Mark as completed
                                            WriteExportStatus(workItem, ExportStatus.Completed);
                                            WriteProgress(workItem, 1);
                                            if (userState.UseVerboseLogging)
                                            {
                                                LogMessage(workItem, "Export file copied to list successfully.");
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            WriteExportStatus(workItem, ExportStatus.Error);
                                            LogMessage(workItem, "Failed reading the exported file: {0}. {1}", e.FileLocation, ex.Message); //this will append the log message
                                        }
                                        finally
                                        {
                                            if (fs != null)
                                            {
                                                fs.Close();
                                            }
                                        }

                                        //Try delete the temporary file, don't change the status to Error if this fails but log it.
                                        try
                                        {
                                            DeleteTempFile(e.FileLocation);
                                        }
                                        catch (Exception ex)
                                        {
                                            if (workItem != null)
                                            {
                                                //the status will still be Completed but an error occurred that should be monitored.
                                                LogMessage(workItem, "Failed to delete temp export file: {0}. ", ex.Message);
                                            }
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                userState.Completed.Set(); //work item has completed
                            }
                        }
                    }
                }
            }
        }