0

I need to read a file in sharepoint document library from node js and store it in a variable. I can read .txt files using sharepoint rest api. sharepoint site/_api/web/ GetFileByServerRelativeUrl('/Shared%20Documents/s_C_locations.txt')/$value.But when I try to read .docx file it returns something as below:

PK!����]�[Content_Types].xml ��(����N�0E�H�C�-j\@B5e�c  H���LZ�_�L��=�@��"Ҳ���s��鋳�$4�b?����B܍.{�"CR�T6x(�P�ww�y�8�c!�D�DJ�Sp
���J�Sįi"�ҏj��?�:xO=�5�pp��Y�.^��I�";{�X{B�h�VĤ�ɗ_\z�9G6{pj"�1��K"L�8W#6�cj��P�^�\�dJ
���z)��_���� Xml/item2.xmlPK-!�H���`customXml/itemProps1.xmlPK-!\�'"�(�acustomXml/_rels/item2.xml.relsPK-!���.j�ccustomXml/item3.xmlPK-!�^����<ecustomXml/itemProps3.xmlPK-!�U�j�OfdocProps/custom.xmlPK-!{���(�hcustomXml/_rels/item3.xml.relsPK��

Please help me to resolve it

Muthu Prasanth
  • 301
  • 5
  • 13
  • this is text representation of a binary file (.zip), everything is fine. What you need is proper viewer, check this answer: https://stackoverflow.com/a/27958186/1498401 – tinamou Apr 26 '18 at 14:57
  • Hi @tinamou,I don't need to render it in a browser.I want to read a file content in node js and store it in a variable. – Muthu Prasanth Apr 26 '18 at 17:05
  • how you understand file content? Word documents (.docx) have fonts, paragraphs, tables, objects, images etc. not only text like .txt files – tinamou Apr 26 '18 at 17:13
  • Yes .docx file contain all those you mentioned. Is there any way to read those files,because in my project I need to read the content and do analysis on that.And another one is if the .docx present in the local(inside my project folder) I can read it using the textract module in node js. But the use case is to read from sharepoint document library – Muthu Prasanth Apr 26 '18 at 17:22
  • 1
    there are `textract` methods to extract from buffer or url – tinamou Apr 26 '18 at 17:37
  • Can you provide a small code using textract to read a file from sharepoint document library – Muthu Prasanth Apr 26 '18 at 17:47

1 Answers1

1

Using encoding: null is a key here.

Here is an example how to download a file from SharePoint correctly with Node.js and REST API.

You can simplify a task and grab ready to use libraries like sppull or sp-download for example.

Cheers, Andrew

  • Thanks for your relpy Andrew. I used encoding:null .It returns as ...sppull is used to download all the documents in the document library.I need to download only one file – Muthu Prasanth Apr 27 '18 at 13:08
  • Andrew, I am using Postman then RestSharp. Encoding:null as a header does nothing. your JS code passes it not in a header. what should the accept and Content-type be in the headers? Thanks – Ofer Gal May 30 '19 at 23:00