You have several ways to download a document, depending on where your code works. The steps are almost the same.
From the server object model
Use this if you are running on the server side of SharePoint (web parts, event listeners, application pages, etc.).
var context = SPContext.Current;
var web = context.Web;
var docLib = web.Lists.TryGetList("NAME OF THE LIBRARY HERE");
if (docLib == null)
{
return;
}
docLib.RootFolder.Files.Add(docLib.RootFolder.Url + "FILE NAME HERE", someFileStream);
Client Code (C #)
Use this if you are running a client application that uses SharePoint services.
ClientContext context = new ClientContext("URL OF THE SHAREPOINT SITE");
var web = context.Web;
var newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes("PATH TO YOUR FILE");
newFile.Url = "NAME OF THE NEW FILE";
var docs = web.Lists.GetByTitle("NAME OF THE LIBRARY");
var uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
JS - SharePoint
, :
clientContext = new SP.ClientContext.get_current();
spWeb = clientContext.get_web();
spList = spWeb.get_lists().getByTitle("NAME OF THE LIST HERE");
fileCreateInfo = new SP.FileCreationInformation();
fileCreateInfo.set_url("my new file.txt");
fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
this.newFile = spList.get_rootFolder().get_files().add(fileCreateInfo);
clientContext.load(this.newFile);
clientContext.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);
, !