Meteor-Community-Packages / Meteor-CollectionFS

Reactive file manager for Meteor
MIT License
1.05k stars 237 forks source link

Buffer on server how to use. #871

Open NaderIkladious opened 8 years ago

NaderIkladious commented 8 years ago

Error: DataMan constructor requires a type argument when passed a Uint8Array

Meteor.methods({
    'saveFile': function(buffer){
      return Images.insert(buffer);
    }
});
var imageId;
if( !!event.target.file.value ) {
      var image = event.target.file.files[0];

      var reader = new FileReader(); //create a reader according to HTML5 File API

      reader.onload = function(event){
        var buffer = new Uint8Array(reader.result) // convert to binary
        Meteor.call('saveFile', buffer, function(id) {
          imageId = id;
        });
      };

      reader.readAsArrayBuffer(image); //read the file as arraybuffer
    }
beeekind commented 8 years ago

Currently dealing with the same problem, any solutions?

nooitaf commented 8 years ago

Buffer is Server-Only.

Also method-calls use DDP and thats much slower then HTTP which gets used if you insert on the client.


https://github.com/CollectionFS/Meteor-CollectionFS#initiate-the-upload

The insert method can directly accept a variety of different file representations as its first argument:

nooitaf commented 8 years ago

Hmm, on second view, you are using Uint8Array which is labeled as "both" so you might just have to add a type somehow.. (might have to do some more digging here)

Also the Meteor.call callback always returns (error, result)

A suggestion in https://github.com/CollectionFS/Meteor-CollectionFS/issues/367 is to use base64.

johhansantana commented 7 years ago

I can't seem to get this to work :(

client side

import axios from 'axios';

const url = "http://localhost:3000/methods/insertImage";

export default function upload(file: File) {
  if (!file) return;

  let reader = new FileReader(); //create a reader according to HTML5 File API

  reader.onload = function(event){
    let buffer = new Uint8Array(reader.result); // convert to binary
    console.log('buffer: ', buffer);
    axios.post(url, buffer);
  };

}

meteor server side

Meteor.methods({
  insertImage: (file) => {
    const fsFile = new FS.File(file);
    Images.insert(fsFile, function (err, fileObj) {
      if (err) throw new Meteor.Error('500', err, err);
      console.log(fileObj);
      return 'success: ' + fileObj;
    });
  }
});

I get DataMan constructor received data that it doesn't support