我有一个可以工作的meteor/cordova应用程序,它使用弹弓上传到AWS/S3。我可以从浏览器和iOS上传和查看应用程序中的照片。
然而,在Android上,我无法从slingshot提供的AWS链接加载照片并将其存储在我的数据库中,当我尝试上传照片时,我得到一个错误消息:
"error : failed to upload file to cloud storage [-0]"有没有什么android特有的东西我错过了/我应该做些什么来为android配置弹弓/我的应用程序?任何帮助都将不胜感激。谢谢!
相关客户端代码(减去文件限制):
//upload to AWS once file is selected
'change #imgUpload' : function(){
var uploader = new Slingshot.Upload("uploadFiles");
var questionId = Session.get('selectedQuestion');
uploader.send(document.getElementById('uploadInput').files[0], function (error, downloadUrl) {
if (error) {
// Log service detailed response
console.log(error)
console.error('Error uploading' );
alert (error);
}
else {
Meteor.call('uploadImage', questionId, downloadUrl);
}
});相关的服务端方法:
'uploadImage' : function(questionId, downloadUrl){
check(questionId, String);
check(downloadUrl, String);
questionsList.update({_id: questionId},
{$set: {
picUrl: downloadUrl
}
});
}, //end upload image相关服务器端指令(减去文件限制):
Slingshot.createDirective("uploadFiles", Slingshot.S3Storage, {
bucket: <bucketname>,
acl: "public-read",
authorize: function(){
//you can't upload if youre not logged in
if(!this.userId){
var message = "Please log in before posting files";
throw new Meteor.Error("Login Required", message);
}
return true;
},
key: function(file){
//store file in a directory based on a users team name
var teamName = Meteor.user().roles.defaultGroup[0]
return teamName + "/" + file.name;
}
});相关mobile_config.js访问规则:
App.accessRule('https://<app-name>.herokuapp.com/*');
App.accessRule('http://<app-name>.herokuapp.com/*');
App.accessRule('https://s3.amazonaws.com/<bucketname>/*');
App.accessRule('http://localhost:3010/*');显示图片的相关模板:
{{#if theQuestion.picUrl}}
<img src="{{theQuestion.picUrl}}" width="300px" height="300px" class="userPic">
{{/if}}上传图片的相关模板:
<template name="uploader">
<form id="imgUpload">
<input id='uploadInput' class="fileLoader" name="file" type="file">
<label for="file" class="uploadWords">Tap here to upload/take a picture</label>
</form>
文件限制:
Slingshot.fileRestrictions("uploadFiles", {
allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});发布于 2016-09-05 10:13:09
如果其他人有这个问题,为了将来参考,我最终去掉了流星弹弓,转而使用https://github.com/Lepozepo/S3。它工作得很好。
对于任何代码,请随时给我发消息,但我基本上使用了示例中提供的样板代码。
发布于 2016-10-21 13:55:14
遇到了同样的问题,我发现是这样的。我们使用Webkit创建的文件对象与Andriod上提供的文件对象不同。Webkit上的文件对象构造函数是
new File([Blob],"FileName.png",{type: "image/png"});与在Cordova上不同的是,为了解决使用'uploader.send‘而不是传递文件对象(由Meteor/Cordova内部创建)直接传递Blob对象时的问题,我用来创建blob对象的代码是
b64toBlob = function(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data.split(',')[1]);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;}我通过的方式最终构建了我的代码
if (Meteor.isCordova) {
console.log("Detected Mobile device");
var blobFile = b64toBlob(data, "image/png");
blobFile.name = "userPhoto.png";
newFile = blobFile;
} else {
newFile = new File([b64toBlob(data, "image/png")], "usePhoto.png", {type: "image/png"});
}
uploader.send(newFile, function (error, downloadUrl) {
//Deal with the download URL
}https://stackoverflow.com/questions/38985191
复制相似问题