Imanuel Gittens
Imanuel Gittens

Reputation: 67

Meteor File Upload Not Working

I've added the packages cfs:standard-packages and cfs:filesystem to my meteor project. I want to upload featured images for my blog using a form with this input.

<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>

And the event 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);
            });
        }
    }
});

The 'uploadFeaturedImage' method on the server is

Meteor.methods({

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

When i choose an image file to upload i get this error - "Exception while invoking method 'uploadFeaturedImage' Error: DataMan constructor received data that it doesn't support"

Anyone have any ideas why this is happening? Thank you.

Upvotes: 2

Views: 1655

Answers (1)

chaosbohne
chaosbohne

Reputation: 2484

I copied some explanation from the collectionFS documentation because it is really good described there.

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.

Have a look at HERE

So your method is not working because no data is sent to the server.

Upvotes: 5

Related Questions