我正在尝试使用Method调用为meteor实现文件上传。
我正在使用这个流星包:https://atmospherejs.com/ostrio/files。
我在客户端没有问题(我可以用base64编码格式发送文件)。在服务器端,我正在尝试实现此功能:https://github.com/VeliovGroup/Meteor-Files/blob/master/docs/write.md
但是我收到了这个错误。
Error during upload: TypeError: Images.write is not a function
以下是我在服务器上的方法的代码:
export const insertImage = new ValidatedMethod({
name: 'images.insert',
validate: new SimpleSchema({
file: { type: String },
}).validator(),
run({ file }) {
Images.write(file, {
fileName: 'sample.png',
type: 'image/png',
}, function (error, fileRef) {
if (error) {
throw error;
} else {
console.log(`${fileRef.name} is successfully saved to FS. _id: ${fileRef._id}`);
}
});
},
});
答案 0 :(得分:1)
根据lib文档,您需要首先使用Images
的实例实例化FilesCollection
,类似于以下内容:
https://github.com/VeliovGroup/Meteor-Files#api-overview-full-api
import { FilesCollection } from 'meteor/ostrio:files';
const Images = new FilesCollection({
collectionName: 'Images',
allowClientCode: false, // Disallow remove files from Client
onBeforeUpload(file) {
// Allow upload files under 10MB, and only in png/jpg/jpeg formats
if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
return true;
} else {
return 'Please upload image, with size equal or less than 10MB';
}
}
});
有关构造函数参数的更多详细信息,请参阅https://github.com/VeliovGroup/Meteor-Files/wiki/Constructor
答案 1 :(得分:0)
我使用了这种语法:
Meteor.call('images.insert', {
file: image
}, (err, res) => {
if (err) {
console.log(`Error during upload: ${err}`);
} else {
console.log(`Upload successfully!`);
}
});