I guess you need not only to upload a file into SharePoint Online but to copy a from one site into another, right?
There are several ways of downloading/uploading a file in SharePoint, you could utilize specific SharePoint APIs like CSOM for that purpose or vanilla .NET capabilities (e.g. HttpClient Class, WebClient Class) as demonstrated below:
var sourceWebUri = new Uri("https://sourcesite.sharepoint.com");
var targetWebUri = new Uri("https://targetsite.sharepoint.com");
var userName = "username@tenant.onmicrosoft.com";
var password = "password";
using(var sourceClient = GetClient(sourceWebUri,userName,password))
{
var fileContent = sourceClient.DownloadData(sourceWebUri + "/documents/SharePoint User Guide.docx");
using(var targetClient = GetClient(targetWebUri,userName,password))
{
targetClient.UploadData(targetWebUri + "/documents/SharePoint User Guide.docx,"PUT" ,fileContent);
}
}
where
private static WebClient GetClient(Uri webUri, string userName, string password)
{
var client = new WebClient();
client.Credentials = GetCredentials(webUri, userName, password);
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
return client;
}
private static SharePointOnlineCredentials GetCredentials(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new SharePointOnlineCredentials(userName, securePassword);
}
How to copy a file between sites in SharePoint Online via CSOM API
SharePoint CSOM API contains File.OpenBinaryDirect method that is intended for downloading a file from SharePoint and File.SaveBinaryDirect method for uplading a file into SharePoint, the following example demonstrates how to copy a file between sites:
public static void CopyFile(ClientContext sourceCtx, string sourceFileUrl, ClientContext targetCtx, string targetFileUrl)
{
if (sourceCtx.HasPendingRequest)
sourceCtx.ExecuteQuery();
using (var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(sourceCtx, sourceFileUrl))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(targetCtx, targetFileUrl, fileInfo.Stream, true);
}
}
Usage
using (var sourceCtx = GetSPOContext(sourceWebUri, userName, password))
{
using (var targetCtx = GetSPOContext(targetWebUri, userName, password))
{
CopyFile(sourceCtx, "/sourcesite/documents/SharePoint User Guide.docx", targetCtx, "/targetsite/documents/SharePoint User Guide.docx");
}
}
where
public static ClientContext GetSPOContext(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new ClientContext(webUri) { Credentials = new SharePointOnlineCredentials(userName, securePassword) };
}
Prerequisites
SharePointOnlineCredentials class:
Represents an object that provides credentials to access SharePoint
Online resources.
is a part of SharePoint Online Client Components SDK