2015-08-13 83 views
0

我用下面的HTML允許用戶上傳圖片:上傳文件到Cloudinary流星

<input class="upload" type="file" id="upload"> 

我有以下方法上傳到Cloudinary:

 cloud : function (source) { 
     cloudinary.uploader.upload(source, function(result) { console.log(result) }, 
     { public_id: "test" }); 

    }, 

將下述檢測輸入並調用方法:

'change #upload': function(event, template) { 
      var imgVal = document.getElementById("upload"); 
      Meteor.call("cloud",imgVal); 
     }, 

我收到此錯誤:

Exception while invoking method 'cloud' TypeError: Object #<Object> has no method 'match' 
I20150813-10:10:38.007(-4)?  at C:\Users\Raj\art\.meteor\local\isopacks\npm-container\npm\node_modules\cloudinary\lib\uploader.js:61:34 
I20150813-10:10:38.007(-4)?  at call_api (C:\Users\Raj\art\.meteor\local\isopacks\npm-container\npm\node_modules\cloudinary\lib\uploader.js:368:22) 
I20150813-10:10:38.008(-4)?  at Object.exports.upload (C:\Users\Raj\art\.meteor\local\isopacks\npm-container\npm\node_modules\cloudinary\lib\uploader.js:58:12) 
I20150813-10:10:38.008(-4)?  at [object Object].Meteor.methods.cloud (app\art.js:132:28) 
I20150813-10:10:38.008(-4)?  at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1) 
I20150813-10:10:38.008(-4)?  at packages/ddp/livedata_server.js:648:1 
I20150813-10:10:38.008(-4)?  at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) 
I20150813-10:10:38.008(-4)?  at packages/ddp/livedata_server.js:647:1 
I20150813-10:10:38.009(-4)?  at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) 
I20150813-10:10:38.009(-4)?  at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1) 
=> Meteor server restarted 

我該怎麼辦才能解決這個問題?

+0

我有以下packages.json文件:'{ 「cloudinary」: 「1.2.2」 }' – lilmessi42

回答

0

圖片上傳不支持Meteor.call功能。

嘗試:

'yourcollection'.allow({ <-- without the ''. 
    insert: function() {return true}, 
    update: function() {return true}, 
}); 

把它放在你的lib/yourcollection.js

+0

這樣做後,我收到此錯誤:'W20150813-10:32:19.667(-4) ? (STDERR)錯誤:一種名爲'/ projects/insert'的方法已經定義好了' – lilmessi42

+0

貌似你已經有一種叫做「cloud」的方法 –

+0

btw你安裝了哪些軟件包?你可以在上面列出它嗎? –

0

cloudinary.uploader.upload預計,第一個參數file是一個字符串。您正在發送一個HTMLInputElement

可以提取所選擇的文件(S)爲從使用files屬性和FileReader輸入元件base64編碼串:(源改編自http://www.html5rocks.com/en/tutorials/file/dndfiles/

'change #upload': function(event, template) { 
    var imgVal = document.getElementById("upload"); 
    var files = imgVal.files; // FileList object 

    // Loop through the FileList and upload each image file. 
    for (var i = 0, f; f = files[i]; i++) { 

     // Only process image files. 
     if (!f.type.match('image.*')) { 
     continue; 
     } 

     var reader = new FileReader(); 

     // Closure to capture the file information. 
     reader.onload = (function(theFile) { 
     return function(e) { 
      Meteor.call("cloud",e.target.result); 
     }; 
     })(f); 

     // Read in the image file as a data URL. 
     reader.readAsDataURL(f); 
    } 
    }, 

請注意cloudinary_npm是爲服務器端操作設計的 - 但是我相信上面的代碼可以工作。