How to upload files in server


#1

in server

 Meteor.methods({
   'upload_files'(file=[]){
        HTTP.post(‘127.0.0.1:8080/upload’, {data:{“upload”:file},headers:{“Content- 
     Type”:“multipart/form-data”}})
 }

throw
the request was rejected because no multipart boundary was found


#2

You could simply encode the file as base64 and pass it you your method, then let the method handle the storage of the file. On the client:

Meteor.call('upload_files', files);

On the server:

import fs from 'fs';

Meteor.methods({
  'upload_files'(files) {
    fs.writeFileSync('someDirectory');    
  }
});

#3

127.0.0.1:8080 is aother server :joy::joy::joy:


#4

Using ostrio:files like this

Collection

import { FilesCollection } from 'meteor/ostrio:files';

export const MyFiles = new FilesCollection({
  allowClientCode: false,
  collectionName: 'my_files',
  storagePath: Meteor.settings.storagePath
});

Client

var fileSelect = $('#fileInput')[0];

if (fileSelect.files && fileSelect.files[0]) {
  const upload = MyFiles.insert({
    file: fileSelect.files[0],
    streams: 'dynamic',
    chunkSize: 'dynamic',
    meta: {
      // Additionnal data..
    }
  }, false);

  upload.on('end', function (error, fileObj) {
    if (error) { console.log (error) }
    else {
      // Success
    }
  });

  upload.start();
}