在Meteor中处理文件上传的规范方式是什么?
在Meteor中处理文件上传的规范方式是什么?
我使用了http://filepicker.io。他们会上传文件,将其存储在您的S3中,并返回文件所在的URL。然后,我只需将该URL插入到数据库中。
将filepicker脚本下载到客户端文件夹中。
wget https://api.filepicker.io/v0/filepicker.js
插入一个文件选择器输入标签
<input type="filepicker" id="attachment">
在启动时进行初始化: Meteor.startup( function() {
filepicker.setKey("YOUR FILEPICKER API KEY");
filepicker.constructWidget(document.getElementById('attachment'));
});
附加事件处理程序
Templates.template.events({
'change #attachment': function(evt){
console.log(evt.files);
}
});
edgee:slingshot
,它非常适合大文件(可直接上传到S3,而无需通过应用服务器)。 - RijkTemplate.myForm.events({
'submit form': function(e, template) {
e.preventDefault();
var file = template.find('input type=["file"]').files[0];
var reader = new FileReader();
reader.onload = function(e) {
// Add it to your model
model.update(id, { $set: { src: e.target.result }});
// Update an image on the page with the data
$(template.find('img')).attr('src', e.target.result);
}
reader.readAsDataURL(file);
}
});
我刚刚使用Meteor.methods和HTML5 File的API实现了文件上传功能,这里是代码实现。请告诉我您的想法。
目前似乎没有与HTTP服务器进行交互或执行任何与HTTP相关的操作的方法。
您唯一能够做的是通过Meteor.methods公开的RPC方法与服务器通信,或直接使用公开的mongoDB API与mongoDB进行通信。
现在有一个新的包:edgee:slingshot。它不会将文件上传到你的 Meteor 服务器,但这样做更好,因为它允许 Meteor 服务器专注于提供 Meteor 应用程序的主要目标,而不是处理昂贵的文件传输。
相反,它将文件上传到云存储服务。目前支持 AWS S3 和 Google Cloud Files,但未来还将支持 Rackspace Cloud Files 和 Cloudinary。
你的 Meteor 服务器只是作为协调器工作。
它也是一个非常多才多艺且轻量级的包。
有一个名为路由器的atmosphere包可实现此功能。
实际上,现在处理文件上传的最佳方式是使用CollectionFS。
这是当前最好的解决方案,它使用collectionFS。
meteor add cfs:standard-packages
meteor add cfs:filesystem
客户:
Template.yourTemplate.events({
'change .your-upload-class': function(event, template) {
FS.Utility.eachFile(event, function(file) {
var yourFile = new FS.File(file);
yourFile.creatorId = Meteor.userId(); // add custom data
YourFileCollection.insert(yourFile, function (err, fileObj) {
if (!err) {
// do callback stuff
}
});
});
}
});
服务器:
YourFileCollection = new FS.Collection("yourFileCollection", {
stores: [new FS.Store.FileSystem("yourFileCollection", {path: "~/meteor_uploads"})]
});
YourFileCollection.allow({
insert: function (userId, doc) {
return !!userId;
},
update: function (userId, doc) {
return doc.creatorId == userId
},
download: function (userId, doc) {
return doc.creatorId == userId
}
});
范本:
<template name="yourTemplate">
<input class="your-upload-class" type="file">
</template>
meteor update
后,你应该进行完善的回归测试。这不仅仅是因为这个包的原因。 - Raz如果您不需要存储非常大的文件,或者只需要短时间存储文件,那么这个简单的解决方案非常适用。
在您的html中...
<input id="files" type="file" />
Template.template.events({
'submit': function(event, template){
event.preventDefault();
if (window.File && window.FileReader && window.FileList && window.Blob) {
_.each(template.find('#files').files, function(file) {
if(file.size > 1){
var reader = new FileReader();
reader.onload = function(e) {
Collection.insert({
name: file.name,
type: file.type,
dataUrl: reader.result
});
}
reader.readAsDataURL(file);
}
});
}
}
});
订阅集合并在模板中呈现链接...
<a href="{{dataUrl}}" target="_blank">{{name}}</a>
虽然这可能不是处理大文件或文件密集型应用程序最稳健或最优雅的解决方案,但如果您想要实现简单的上传、下载/渲染各种文件格式,它非常适用。
这里有另一个解决方案:
https://doctorllama.wordpress.com/2014/11/06/meteor-upload-package-with-jquery-file-upload/
这个方案使用Blueimp的上传解决方案,支持分块上传、进度条等功能。