Search code examples
rubyform-datafaye

Sending files with Faye


Is it possible to send files with Faye Ruby server?

Looks like FormData works only by XHR because e.g.:

var fd = new FormData();
fd.append('file', $('.file')[0].files[0]);

client.publish(channel, { file: fd });

returns an empty hash in my Ruby application.


Solution

  • Made using FileReader.

    var sendMessage = function(file) {
      client.publish(channel, { file: file });
    }
    
    var fr = new FileReader();
    
    fr.onload = function(event) {
      sendMessage(event.target.result);
    };
    
    fr.readAsDataURL($('.file')[0].files[0]);