Search This Blog

Monday, March 16, 2015

Code For File uploads

<asp:FileUpload ID="FileUploadAttachment" runat="server" />
And in your update function, you can upload the attachment by:

            SPListItem studentDetailsItem = SPContext.Current.Web.Lists["Student Details"].Items.Add();
            studentDetailsItem["StudentID"] = Guid.NewGuid();
            studentDetailsItem["FirstName"] = txtFirstName.Text;
            studentDetailsItem["LastName"] = txtLastName.Text;


            //------------------------------------------------------------
            // If file selected, save to list
            if (FileUploadAttachment.HasFile == true)
            {
                SPAttachmentCollection attach = studentDetailsItem.Attachments;
                String fileName = FileUploadAttachment.PostedFile.FileName;
                byte[] textfile = FileUploadAttachment.FileBytes;
                attach.Add(fileName, textfile);

            }
            // ------------------------------------------------------------

            studentDetailsItem.Update();

No comments:

Post a Comment