Slingshot 非常适合。
您需要从画布 (use this method) 中提取 Blob,然后将其传递给 slingshot:
function uploadImage(directive, canvas, type, callback) {
var uploader = new Slingshot.Upload(directive);
canvas.toBlob(function (blob) {
blob.type = type;
upload.send(blob, callback);
}, type);
return uploader;
}
uploadImage("myDirective", document.getElementById("mycanvas"), "image/png", function (error, downloadUrl) {
console.log("file is now at %s", downloadUrl);
});
现在服务器端可能是(但这真的取决于你真正想要什么):
Slingshot.create("myDirective", Slingshot.S3Storage, {
allowedFileTypes: "image/png"
acl: "public-read",
authorize: function () {
//Deny uploads if user is not logged in.
if (!this.userId) {
var message = "Please login before posting files";
throw new Meteor.Error("Login Required", message);
}
return true;
},
key: function (file) {
//Store file into a directory by the user's username.
var user = Meteor.users.findOne(this.userId);
return user.username + "/canvas.png";
}
});
如果您想支持 pdf 上传,您只需将 application/pdf 添加到 allowedFileTypes 或创建一个单独的指令来授权 pdf 上传。
您可以在 slingshot at this unreleased branch 上找到一些更好的文档。