在MongoDB‘证书’集合中有'_id,studentID,file‘字段。我只需输入每一个文档,其中'_id‘和'studentID’的数据类型是字符串,而文件数据类型是二进制的(这个二进制数据类型由MongoDB在插入每个用户pdf文件时自动生成)。
从获取的数据,我可以显示成角(如StudentID,SchoolName),但只有我不能显示从获取的二进制数据到角
在节点服务器中:(app.js) //给定核心代码,以避免过长时间的读取
//用于将数据插入到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 }
certificate.model.ts:文件
export class Certificate {
_id: string;
dateTime: string;
studentID: string;
schoolName: string;
file: any;
}
然后在前端角使用(user.service.ts)接收来自节点的所有信息(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[];
});
}
在证书组件中:(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>
每行将显示ID、StudentID、学校名称和按钮(“查看为pdf”),当单击按钮时,弹出窗口或新窗口将显示从获取的pdf二进制数据中获取的pdf文件。
发布于 2019-01-29 04:31:15
正如在评论中发布的那样,您可以使用this answer来解决您的问题,只需进行以下修改:
在开始时,您必须将数据转换为blob。就你而言,这将是:
public getPDFFromFile(file /* place here the correct type of your class*/): Blob {
return new Blob([file.buffer])
}
那么你应该能够使用上面提到的答案。正如MongoDB文档所指出的,您可以通过buffer
属性访问内部二进制数据,该属性的类型为Buffer
。
https://stackoverflow.com/questions/54403860
复制相似问题