var webClient = new WebClient();
string imageUrl = retriveResponse.AbsoluteUrl.ToString() + "/test.jpg";
var webclient = new WebClient();
webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//webclient.Headers.Add(HttpRequestHeader.Accept, "application/octet-stream");
//webclient.Headers.Add("binaryStringRequestBody", "true");
webclient.Credentials = new NetworkCredential(WebConfigurationManager.AppSettings["Username"],
WebConfigurationManager.AppSettings["Password"]);
imageBytes = webclient.DownloadData(imageUrl);
webclient.Dispose();
1 Answer
Try using the following commands:
Ensure you are using SharePoint Online SDK (Microsoft.SharePointOnline.CSOM).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Microsoft.SharePoint.Client;
using System.Security;
namespace DownloadSPOImage
{
class Program
{
static void Main(string[] args)
{
const string username = "<username>@<tenant>.onmicrosoft.com";
const string password = "<password>";
const string url = "https://<tenant>.sharepoint.com/<site>";
string filename = "<name>.jpg";
string imageUrl = url+"/<library>/"+filename;
string destinationLocalFolder = "C:\\temp\\images";
string destinationLocal = destinationLocalFolder + "\\" + filename;
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);
var client = new WebClient();
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
client.Credentials = credentials;
client.DownloadFile(imageUrl, destinationLocal);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", filename, imageUrl);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + destinationLocalFolder);
}
}
}
string downloadUrl = _siteURl + "/_layouts/download.aspx?SourceUrl=" + _path;
Just construct this path for each file and then open it in a new window and the file will be downloaded automatically.