Graph API to upload files to Sharepoint

Jagan 21 Reputation points
2022-01-04T18:35:54.737+00:00

Hi,

I have a requirement to upload set of files (file types: pdf, images, docx, xls, ppt, csv) to Sharepoint.

After setting up with all creds (client id, access token and secret), i'm able to successfully upload txt and rtf files to Sharepoint from our application. The issue is when i try to upload pdf, images, docx, xls, ppt, csv files, upload succeeds, but when i click on the files, **it comes out blank**

When i try it from Postman (using binary to select file), it works fine.

Details
File data: Base64 value
For the binary data: I'm using atob to convert from base64 to binary.
Framework: Angular JS

Any help will be highly appreciated.

Many Thanks!!!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,550 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,662 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 30,991 Reputation points Microsoft Vendor
    2022-01-05T02:43:36.603+00:00

    Hi @Jagan ,
    You can try the following code to convert base64 to binary.

    const b64toBlob = (b64Data, contentType='', sliceSize=512) => {  
      const byteCharacters = atob(b64Data);  
      const byteArrays = [];  
      
      for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {  
        const slice = byteCharacters.slice(offset, offset + sliceSize);  
      
        const byteNumbers = new Array(slice.length);  
        for (let i = 0; i < slice.length; i++) {  
          byteNumbers[i] = slice.charCodeAt(i);  
        }  
      
        const byteArray = new Uint8Array(byteNumbers);  
        byteArrays.push(byteArray);  
      }  
      
      const blob = new Blob(byteArrays, {type: contentType});  
      return blob;  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



0 additional answers

Sort by: Most helpful