1

In MongoDB 'certificate' collection have '_id, studentID, file' fields. Simply I can input for each document where data type for '_id' and 'studentID' are the string, and file data type is Binary (this binary auto-generated by MongoDB during insertion for each user pdf file).

From fetched data, I can display into angular (e.g StudentID, SchoolName) but only I can't display pdf from fetched binary data into angular

In node server: (app.js) // given the core code to avoid too long reading

// This for inserting data into MongoDB

**var fs = require('fs');  
var pdfBinary = fs.readFileSync("./TestingPurpose.pdf"); 
var pdf = new mongodb.Binary(pdfBinary);**
db.collection('Test').insert({                    
                 dateTime: new Date(dateTime),
                 studentName: studentName,
                 Principal: principalName,
                 file: pdf }
  • After successfully data importing in MongoDB can see data in MongoDB as below:

    { "_id" : "C1135", "dateTime" : ISODate("2019-01-23T11:45:52.254+01:00"), "studentID" : "stu123", "schoolName" : "XXX Primary School", "file" : BinData(0,"JVBERi0xLjcNCiW1tbW1DQoxIDAgb2Jq... more 101410 bytes - application/pdf") }


certificate.model.ts: file

export class Certificate {
  _id: string;
  dateTime: string;
  studentID: string;
  schoolName: string;
  file: any;
}

Then in Frontend Angular use (user.service.ts) to receive all infro from Node (app.js)::

import { Certificate } from '../model/certificate.model';
cert: Certificate[];
  // receiving all students info
getCertificates() {
    return this.http.get('http://localhost:5600/aziz/displayAllCertificates');
  }
// to get always get an instant update I use refreshGetCertificates()
  refreshGetCertificates() {
    this.chkSubscrip = this.getCertificates().subscribe((res) => {
      this.cert= res as Certificate[];
    });
  }

In Certificate Component: (certificate.component.ts):

export class CertificatesComponent implements OnInit, OnDestroy {
      myPDF: any;
      constructor(
         public userService: UserService,
         private dialog: MatDialog,
  ) { }
ngOnInit() {
    this.userService.refreshGetCertificates();
  }
 pdfView(receiveObj) {

    this.forPDF = !this.forPDF;
    const myUint8 = new Uint8Array(receiveObj);
    this.myPDF= myUint8;
  }
}

(certificate.component.html):

<div class="rows">
  <ul class="settings_test headerclass text-center">
    <li style="float: left">ID</li>
    <li>Student-ID</li>
    <li>School</li>
    <li>PDF View</li>
  </ul>
</div>

<div *ngFor="let eachCerts of userService.cert">
  <div class="row">
    <div class="settings_test">
      <li style="width:10%">{{eachCerts._id}}</li>
      <li style="width:10%">{{eachCerts.studentID}}</li>
      <li style="width:10%">{{eachCerts.schoolName}}</li>
      <li style="width:10%"> <button (click) ="pdfView(eachCerts.file)">  View as PDF </button></li>
      <object *ngIf=forPDF data="{{myPDF}}" ></object>
    </div>
  </div>
</div>

In each row will show ID, StudentID, SChool-Name and a button('View as PDF'), when the button will be clicked then a popup or a new window will appear to display the pdf file from the fetched pdf binary data.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
Apon
  • 11
  • 1
  • 5
  • So, you are basically saying that you cannot open the PDF in a new browser tab or download it at all? What is actual behavior or output? – Chris Tapay Jan 28 '19 at 14:24
  • 1
    Thanks Yes, you got me. In console show me about error: ERROR Error: unsafe value used in a resource URL context (see http://g.co/ng/security#xss)......at DomSanitizerImpl.push../node_modules/@angular/platform-browser/fesm5/platform-browser.js.DomSanitizerImpl.sanitiz ____ Sorry, I am not knowledgable in Angular, so do not know exactly how to say about the error. I think about url trust issue. – Apon Jan 28 '19 at 14:30
  • This question should help you: https://stackoverflow.com/questions/52154874/angular-6-downloading-file-from-rest-api/52687792#52687792 – Yennefer Jan 28 '19 at 15:54
  • Possible duplicate of [Angular 6 Downloading file from rest api](https://stackoverflow.com/questions/52154874/angular-6-downloading-file-from-rest-api) – Yennefer Jan 28 '19 at 15:54
  • @Pier, First of all, I have not that much knowledge of programming but I am learning and love to do. I have seen your given link but it didn't work for me. I get an object from REST API and in the object, there has an element that keeps binary data (data type of string) for pdf. And then I need to display the pdf. Help me, please. I am totally lost for that. – Apon Jan 29 '19 at 10:09
  • 1
    Ok, what I posted works for binary data. You may recycle everything I gave to you by converting the "string containing the binary data" into truly binary data. How is the string encoded? Base64? – Yennefer Jan 29 '19 at 10:36
  • @Pier Thanks for your efforts but unfortunately I couldn't manage it. the pdf encoded as **var pdfBinary = fs.readFileSync("./TestingPurpose.pdf"); var pdf = new MongoDB.Binary(pdfBinary);** I will post the sequence simply with short and details now. Really a new and poor programmer it is killing me and I think it quite complicated too :( – Apon Jan 29 '19 at 11:58
  • I updated the answer. Check if it fits now. – Yennefer Jan 29 '19 at 12:25

1 Answers1

0

As posted in the comments, you can use this answer that should solve your issue, with this modification:

At the beginning, you have to convert your data into a blob. In your case that would be:

public getPDFFromFile(file /* place here the correct type of your class*/): Blob {   
    return new Blob([file.buffer])
}

then you should be able to use the above mentioned answer. As note by MongoDB documentation, you can access the internal binary data via the buffer property which is of type Buffer.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
  • sure if you had the chance to look on my code it would be solved a long time before. Think so because of my lack of knowledge in programming I am suffering from that. I am trying if I can't then I will try to upload in git to share the complete code then kindly help me to solve it, please. I feel so shy and guilty to bother you guys and thanks in advance. :) – Apon Jan 29 '19 at 15:24