Multiple Files Upload Control in SharePoint

Multiple Files Upload Control in SharePoint
7 votes, 5.00 avg. rating (98% score)

Introduction

Looking for multiple file upload control in SharePoint 2010 or in SharePoint 2013. This article will provide you code snippet, webpart to download and full source code project

Skill Level – Medium


Details

Aspx page code
Refers jquery library file and opensource Multifile.js for displaying uploaded files in bettter UI

<script type="text/javascript" src="/_layouts/MultifileUpload/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/MultifileUpload/jquery.MultiFile.js"></script>
<div>
<h3>Upload Attachments</h3>
</div>

 


User interface will be converted after adding js files

image

image

Upload file code to insert multiple items into custom list

		///
<summary> /// Code to upload attachments into list 'TestAttachments'
 /// </summary>
        protected void btnUpload_click(object sender, EventArgs e)
        {
            try
            {
                SPWeb webObj = SPContext.Current.Web;
                //change the list name here
                SPList lstObj = webObj.Lists.TryGetList("TestAttachments");
                if (lstObj != null)
                {
                    SPListItem thisItem = lstObj.AddItem();

                    thisItem["Title"] = "This is test by Adi"; //this is only for testing

                    if (Request.Files.Count > 0)
                    {
                        SPAttachmentCollection attach = thisItem.Attachments;
                        HttpFileCollection uploadedFiles = Request.Files;
                        Stream attachmentStream;
                        Byte[] attachmentContent;
                        for (int i = 0; i < uploadedFiles.Count; i++)
                        {
                            HttpPostedFile userPostedFile = uploadedFiles[i];
                            String fileName = userPostedFile.FileName;
                            attachmentStream = userPostedFile.InputStream;
                            attachmentContent = new Byte[attachmentStream.Length];
                            attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);

                            attachmentStream.Close();
                            attachmentStream.Dispose();

                            // Add the file to the attachment collection
                            attach.Add(fileName, attachmentContent);
                        }

                    }

                    thisItem.Update();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message); // put your custom error handling here
            }
        }

 

When we hit upload, the uploaded files will be saved to ShaerPoint custom list. Here I have used my custom list ‘TestAttachments’.

image

SourceCode

Download Source Code

Conclusion

Uploading single file is a bit easy but provisioning facility to upload multiple files is a bit tricky. Hope the codes saves your time rather than exploring how to achieve the requirement. Happy coding.

October 9, 2014 В· Adi В· 4 Comments
Tags: ,  В· Posted in: Sharepoint 2010, SharePoint 2013, Tools

4 Responses

  1. vishal wattamwar - January 8, 2016

    Hi,

    This code is working perfectly.
    But when we refresh the page before upload button clicks,it losses the attached file.

    Please do the needful.

  2. Adi - January 29, 2016

    Yes, the file upload control is client side management and does not manage state. You can tweak it by using hidden controls to save the uploaded control file list. In the page refresh try to fill the controls again with hidden control data.

    Regards,
    Adi

  3. Alex - May 4, 2016

    Hey, there Adi, thanks for posting this. Could you please test this out with files larger than 100 KiB or 1 MiB? Im getting errors when file size is larger than 80 Kilobytes, id like to know if you have same issues here? Thank you!

  4. Adi - May 4, 2016

    Hi Alex,
    There is no limit on the size, it perfectly worked with more than 500MB data file also. May be there is limit set on IIS or any other setting from your end.

    Regards,
    Adi

Leave a Reply

What is 14 + 10 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)