2014-12-10 3 views
2

Я добавил пакеты cfs: standard-packages и cfs: filesystem к моему проекту meteor. Я хочу загрузить избранные изображения для своего блога, используя форму с этим вводом.Загрузка файла Meteor не работает

<div class="form-group"> 
     <label for="featuredImage">Featured Image</label> 
     <input type="file" id="fImage" required> 
     <p class="help-block">Please choose an image file.</p> 
</div> 

И событие Javascript

Template.AddPost.events({ 
    'change #fImage': function(event, template) { 

     var image = template.find('[id=fImage]').value; 
     var lastIndex = image.lastIndexOf("\\"); 
     if (lastIndex >= 0) { 
      image = image.substring(lastIndex + 1); 
     } 
     if (!image.match(/\.(jpg|jpeg|png|gif)$/)) { 
      alert("not an image"); 
     } else { 
      FS.Utility.eachFile(event, function(file) { 
       var fileObj = new FS.File(file); 
       Meteor.call('uploadFeaturedImage', fileObj); 
      }); 
     } 
    } 
}); 

'uploadFeaturedImage' метод на сервере

Meteor.methods({ 

    'uploadFeaturedImage': function(fileObj){ 
     Uploads.insert(fileObj, function(err){ 
      console.log(err); 
     }); 
    } 
}); 

Когда я выбрать файл изображения для загрузки я получаю эту ошибку - «Exception при вызове метода 'uploadFeaturedImage' Ошибка: конструктор DataMan получил данные, которые он не поддерживает "

У кого-нибудь есть идеи, почему это происходит? Спасибо.

ответ

5

Я скопировал некоторые объяснения из документации коллекцииFS, потому что это действительно хорошо описано там.

When you need to insert a file that's located on a client, always call myFSCollection.insert on the client. While you could define your own method, pass it the fsFile, and call myFSCollection.insert on the server, the difficulty is with getting the data from the client to the server. When you pass the fsFile to your method, only the file info is sent and not the data. By contrast, when you do the insert directly on the client, it automatically chunks the file's data after insert, and then queues it to be sent chunk by chunk to the server. And then there is the matter of recombining all those chunks on the server and stuffing the data back into the fsFile. So doing client-side inserts actually saves you all of this complex work, and that's why we recommend it.

Посмотрите HERE

Так что ваш метод не работает, потому что никакие данные не передаются на сервер.

+0

Большое вам спасибо! это сработало отлично. Имейте большой день sir :) –

+0

Как вы определяете, когда загрузка будет готова при вызове Images.insert на клиенте. он обратный вызов срабатывает сразу, даже если файлObj.url() не определен? – daslicht

Смежные вопросы