Meteor-CollectionFS
Meteor-CollectionFS copied to clipboard
Buffer on server how to use.
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
}
Currently dealing with the same problem, any solutions?
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:
- File object (client only)
- Blob object (client only)
- Uint8Array
- ArrayBuffer
- Buffer (server only)
- A full URL that begins with "http:" or "https:"
- A local filepath (server only)
- A data URI string
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.
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