我有这个事件(使用< input type =“file”>上传图像文件):
"change .logoBusinessBig-upload":function(event,template){ var reader = new FileReader() reader.addEventListener("load",function(evt){ var x = reader.result console.log(x) Meteor.call("saveFile",x) }) reader.readAsArrayBuffer(event.currentTarget.files[0]) }
而这个Meteor.method()
saveFile:function(file){ console.log(file) var fs = Npm.require("fs") fs.writeFile('../../../../../public/jow.txt',file,function (err) { console.log("file saved") }); }
事件中的console.log(x)输出一个ArrayBuffer对象,而Meteor.method()中的console.log(文件)显示并清空{}对象.
这是为什么? ArrayBuffer应该已经传递给Meteor.method()
解决方法
//client.js 'change': function(event,template) { event.preventDefault(); var file = event.target.files[0]; //assuming you have only 1 file 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); } reader.readAsArrayBuffer(file); //read the file as arraybuffer } //server.js 'saveFile': function(buffer){ fs.writeFile('/location',new Buffer(buffer),function(error){...}); }
您无法保存到/ public文件夹,这会触发重新加载