我使用这个函数在集合中保存了一个 pdf 文件:
/*** client.js ***/
// asign a change event into input tag
'change input' : function(event,template){
var file = event.target.files[0]; //assuming 1 file only
if (!file) return;
var reader = new FileReader(); //create a reader according to HTML5 File API
reader.onload = function(event){
var buffer = new Uint8Array(reader.result) // convert to binary
Meteor.call('saveFile', buffer);
}
reader.readAsArrayBuffer(file); //read the file as arraybuffer
}
/*** server.js ***/
Files = new Mongo.Collection('files');
Meteor.methods({
'saveFile': function(buffer){
Files.insert({data:buffer})
}
});
如何从集合中再次读取它并提供下载链接,用户可以将文件下载为 pdf 并将其保存在本地计算机上?
最佳答案
这取决于当您在 MiniMongo 集合中看到该文档记录时,前端的数据类型最终是什么。您要做的是将该 Uint8Array 数据转换为 base64 编码的数据 URL 并提供 <a href="data:application/pdf;base64,...">Download PDF</a>
在浏览器中获取数据后链接。
Meteor 不支持开箱即用的服务器文件服务,因此您可能必须通过 mongo->minimongo 发布/订阅机制发布该文件的 blob,然后使用 HTML data-uri API 来获取它我刚刚描述过。
关于mongodb - 从 Collection 中读取 Uint8Array 缓冲区并下载为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44246786/