Prerequisites
We need to have the AJAX Control toolkit installed.
- Create an ASP.NET web application.

- Add SharePoint DLL References. These references can be found in “C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\15\ISAPI”.
In order to have this dll you need to install SharePoint Client SDK “https://www.microsoft.com/en-us/download/details.aspx?id=35585”

- Now on your web form, you need to drag AjaxFileUpload Controland ScriptManager.

- Now on OnUploadComplete create an Event.

And paste the following code.
- protectedvoidOnUploadComplete(object sender, AjaxFileUploadEventArgs e)
- {
- try
- {
- //gets the filename
- stringfileName = Path.GetFileName(e.FileName);
- //gets the temporary location of file and ID of folder where files are stored in temporary folder
- string path = Path.Combine(Path.GetTempPath(), "_AjaxFileUpload", e.FileId);
- //store the path in Sessio
- Session["FileDirectory"] = path;
- //get the site
- ClientContext context = newClientContext(siteUrl);
- //get the web object
- WeboWebsite = context.Web;
- context.Load(oWebsite);
- //execute the web object
- context.ExecuteQuery();
- //get the current list
- ListCurrentList = context.Web.Lists.GetByTitle(listName);
- context.Load(CurrentList.RootFolder);
- context.ExecuteQuery();
- //form the url to get the entire path of file like D://folder/test.txt
- StringfileURL = (path + "\\" + fileName);
- using(FileStreamfileStream = newFileStream(fileURL, FileMode.Open))
- Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, CurrentList.RootFolder.ServerRelativeUrl.ToString() + "/" + e.FileName, fileStream, true);
- }
- catch (Exception ex)
- {
- lblError.Text = ex.ToString();
- lblError.BackColor = System.Drawing.Color.Red;
- lblError.ForeColor = System.Drawing.Color.White;
- }
- finally
- {
- //delete files from temp folder
- e.DeleteTemporaryData();
- }
- }
- Now run the application.



Files are now uploaded to SharePoint.



Kola OwolabiPosted Jul 29, 2021, 9:47 AM
Hi, Thanks for this. I ran the code but it shows uploaded for the first file and pending for others. nothing appears in the library. Can you help?
Humayun Kabir MamunPosted Jun 18, 2016, 12:54 PM
Nice...