1

I have to get some image data from SharePoint library in my server side console app.

However, i only find the some download to PC solution in internet, so how get the image directly.

my code

foreach(SPListItem item in docLib.Items)
{
  // Access the file
  SPFile file = item.File;
  if(file != null)
  {
    // retrieve the file as a byte array
    byte[] bArray = file.OpenBinary();
    string filePath = Path.Combine("c:\\temp", file.Name);
    //open the file stream and write the file
    using(FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
    {
      fs.Write(bArray, 0, bArray.Length);
       MemoryStream ms = new MemoryStream(bArray)
      Image myImage = Image.FromStream(ms); // i get my image now, but i have to download it to PC first.
    }
  }

}
  • What image data do you need? – Jussi Palo Aug 29 '16 at 5:18
  • @JussiPalo the data that i can , for example, use myImage.Save(@"C:\Users\HIHI\Downloads\asd.TIFF", System.Drawing.Imaging.ImageFormat.Png); – king yau Aug 29 '16 at 5:24
4

I have achieved it by using following code.Hope it will help you.

      public static string GetImageBinary(SPWeb web, string name)
      {
        string Logo = string.Empty;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                using (SPSite site = new SPSite(web.Url))
                {
                    using (SPWeb rootWeb = site.RootWeb)
                    {
                        SPList spList = rootWeb.Lists.TryGetList(Constants.LISTNAME_SITEASSESTS);
                        if (spList != null)
                        {
                            SPQuery query = new SPQuery();
                            query.Query = "<Where><Eq><FieldRef Name='LinkFilename' /><Value Type='Text'>" + name + "</Value></Eq></Where>";
                            SPListItemCollection files = spList.GetItems(query);

                            if (files.Count > 0)
                            {
                                byte[] image = files[0].File.OpenBinary(SPOpenBinaryOptions.None);
                                Logo = Convert.ToBase64String(image);
                            }
                        }
                    }
                }

            });
        }
        catch
        {
            throw;
        }
        return Logo;
    }
|improve this answer|||||
  • O~ thanks, it work :) So that the byte[] image= sp_item.File.OpenBinary(); will give me the data of the image? – king yau Aug 29 '16 at 6:17
  • Yes. My requirement was to convert image into base64 string.so that i can use it as data url. – Akshay Belure Aug 29 '16 at 6:31
2

First suggestion, as soon as you have the absolute URL of the file in SharePoint, use this:

    WebClient wc = new WebClient();
    using (MemoryStream stream = new MemoryStream(wc.DownloadData("FILE_URL")))
    {
        // Do something with the stream
    }
|improve this answer|||||
  • I cannot use webLient, it brings an Unauthorized error.. I have tried it already. – king yau Aug 29 '16 at 5:33
  • Unauthorized indicates that the requested resource requires authentication. – king yau Aug 29 '16 at 5:34
  • Use Akshay's code, it will work. – Jussi Palo Aug 29 '16 at 6:11

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.