0

I would like to upload multiple files in Sharepoint "Shared Documents" with user control this is my ascx :

 <asp:FileUpload ID="fileBrowse" runat="server" />
 <asp:Button ID="fileUpload" runat="server" Text="Upload Files" OnClick="fileUpload_Click" />

and my code behind :

  protected void fileUpload_Click(object sender, EventArgs e)
    {
        if (fileBrowse.HasFile)
        {
            string filename = fileBrowse.PostedFile.FileName;
          //  string allpath = Path.GetFileName(fileBrowse.PostedFile.FileName);
         //   string all = Path.GetFullPath(fileBrowse.PostedFile.FileName);
            String fileToUpload = @filename;
            String sharePointSite = "http://.../sites/GM/";
            String documentLibraryName = "Shared Documents";

            using (SPSite oSite = new SPSite(sharePointSite))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    if (!System.IO.File.Exists(fileToUpload))
                        throw new FileNotFoundException("File not found.", fileToUpload);

                    SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                    // Prepare to upload
                    Boolean replaceExistingFiles = true;
                    String fileName = System.IO.Path.GetFileName(fileToUpload);
                    FileStream fileStream = File.OpenRead(fileToUpload);

                    // Upload document
                    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

                    // Commit 
                    myLibrary.Update();
                }
            }
        }
    }

it just upload 1 file, and it works in dev machine, not client machine "File not found"

Help please

3
  • You can refer my code on the below link
    – Sunil Sahu
    Commented Sep 26, 2016 at 8:21
  • @Sunilsahu, could you please help me to understand why it's not working ?
    – BKChedlia
    Commented Sep 26, 2016 at 9:14
  • you have to allow multiple files on fileupload control and loop through the files users selected..... this is supported in .net 4.0...else you need to have multiple file upload control with your button control click reading each file upload controls and uploading to sharepoint... Commented Sep 26, 2016 at 9:20

1 Answer 1

0

Please add AllowMultiple="true" in your file uploader control

And in code use

if (fileBrowse.HasFiles)
{
foreach (HttpPostedFile uploadedFile in fileBrowse.PostedFiles)
   {
 /// your code for file upload.
   }
}
6
  • Sunil sahu, I will try it now
    – BKChedlia
    Commented Sep 26, 2016 at 11:33
  • but could you please , explain to me . why in the dev machine to load 1 file, it works , but in the local machine, it said file not found for the same user ?
    – BKChedlia
    Commented Sep 26, 2016 at 11:37
  • error message :foreach statement cannot operate on variables of type 'System.Web.HttpPostedFile' because 'System.Web.HttpPostedFile' does not contain a public definition for 'GetEnumerator'
    – BKChedlia
    Commented Sep 26, 2016 at 11:44
  • Are you using update panel
    – Sunil Sahu
    Commented Sep 26, 2016 at 12:26
  • no I m not. I m putting my controls in a div,
    – BKChedlia
    Commented Sep 26, 2016 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.