Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How get base64 string from the application/octet-stream using CRM Plugin or javascript?

Posted on by Microsoft Employee

Hi,

I have to read file(pdf) from sharepoint and In REST API response I am getting data with  of type application/octet-stream I need to create file stream and create file(pdf).

Thanks!

*This post is locked for comments

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How get base64 string from the application/octet-stream using CRM Plugin or javascript?

    I had the API hosted on Azure, which is why I didn't have the issue with Sandboxed environment, and restricted only for CORS to the CRM server. Not sure if this is an option for you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How get base64 string from the application/octet-stream using CRM Plugin or javascript?

    I have checked for same but, ClientContext way throwing permission exceptions for sharepoint online credential object.

    Some others developers are also not suggesting this way as it's not supported on sand box.

    Please share other options..

    Thanks for quick response!

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How get base64 string from the application/octet-stream using CRM Plugin or javascript?

    This might be something else to look at, but maybe an options:

               string contentType = System.Web.MimeMapping.GetMimeMapping(fileName);

               HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

               Byte[] bytes = downloadFile(item);

               response.Content = new ByteArrayContent(bytes);

               response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

               response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

               response.Content.Headers.ContentDisposition.FileName = fileName;

    The download file function looks like this. This was implemented for SharePoint, so you would have to modify accordingly to your requirements.

           public static byte[] DownloadFile(ListItem item)

           {

               ClientContext ctx = ConnectToSharePoint();

               List spList = ctx.Web.Lists.GetByTitle("Documents");

               ctx.Load(spList);

               ctx.ExecuteQuery();

               ClientResult<System.IO.Stream> result = item.File.OpenBinaryStream();

               ctx.ExecuteQuery();

               using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

               {

                   result.Value.CopyTo(ms);

                   return ms.ToArray();

               }

           }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans