Skip to content Skip to sidebar Skip to footer

Sapui5/javascript How To Convert Binary Data To Readable Format And Download As Pdf

Im getting Binary data from OData Service.Can someone please help me how to convert Binary data to Readable format. Im trying with few options like atob But it is not getting worke

Solution 1:

Well, there are several ways to go about it. The most important piece of information is that the string you have there is a Base64 encoded version of your PDF file.

You can create a Blob out of it and download it using some small "tricks" (for IE, you have a IE specific API, for the rest of the browsers you can use a data URL). Please check the following S.O. question: Download base64 data using javascript | IE11. The gist of it is the following:

var a = window.document.createElement("a");
 a.href = window.URL.createObjectURL(blob, { type: "application/pdf" });
 a.download = "filename.pdf";
 document.body.appendChild(a);
 a.click(); 
 document.body.removeChild(a);

Or you can use the built-in UI5 utility function sap.ui.core.util.File.save and pass it the decoded binary data:

sap.ui.core.util.File.save(bin, "filename", "pdf", "application/pdf");

Of course, another possibility would be to use a third-party library like download.js:

download(bin, "filename.pdf", "application/pdf");

In my opinion, you should go with the UI5 version, because:

  • The first version makes you write some fairly low-level, browser-dependent code.
  • If you look at the source code of the UI5 function, it actually does mostly the same thing.
  • The last version adds an external dependency which can be avoided.

The only thing to look out for is the compatibility of the UI5 function; a third-party library specialized in this thing might have a better compatibility matrix:

macOS Safari < 10.1 / iOS Safari A new window or tab is opened. In macOS, the user has to save the file manually (by using key combination "CMD + S", choosing the page source format, and specifying a file name). In iOS, the content can be opened in another app (Mail, Notes, ...) or can be copied to the clipboard. If a pop-up blocker prevents this action, an error will be thrown which can be used to notify the user that the pop-up blocker needs to be disabled.

Android Browser Not supported

Windows Phone 10 Edge Not supported

Post a Comment for "Sapui5/javascript How To Convert Binary Data To Readable Format And Download As Pdf"