0
            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();
2
  • I am using above code but on conversion of ByteArray to Image gives broken Image. I think I am doing something wrong while retrieving the image.
    – DK21
    Aug 5 '18 at 10:59
  • 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. Aug 6 '18 at 11:18
0

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);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.