string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
I'am Using this code from MSDN Web Site
But I have coming across the error: 403 forbidden
Can someone Help me Put this working ?
On your OneDrive or SharePoint Online website, select the files or folders you want to download. Select Download. If your browser prompts you, choose Save or Save As and browse to the location where you want to save the download. Some browsers just start saving right away to a Downloads folder on your computer.
Login to your SharePoint Online Site, Navigate to the Folder you want to download. Right-click on the Folder >> Choose “Download” from the context menu. You can also use the “Download” button from the toolbar. This brings you a Zip file with the Selected folder and its Sub-Folders and its files.
You can download files and folders from Microsoft OneDrive, or from SharePoint in Microsoft 365, SharePoint Server Subscription Edition, or SharePoint Server 2019 to your computer with just a few clicks.
Find the file you want to share in OneDrive or SharePoint, and select the circle in the upper corner. Select Share at the top of the page. Select Anyone with the link can edit to open Link settings. Uncheck Allow editing, and then turn on Block download.
I was facing the same issue and tried the answer suggested by Vadim Gremyachev. However, it still kept giving 403 error. I added two extra headers to force form based authentication like below:
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
After this it started working. So the full code goes like below:
const string username = "[email protected]";
const string password = "password";
const string url = "https://tenant.sharepoint.com/";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);
DownloadFile(url,credentials,"/Shared Documents/Report.xslx");
private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl)
{
using(var client = new WebClient())
{
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
client.Credentials = credentials;
client.DownloadFile(webUrl, fileRelativeUrl);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With