Javascript 使用fs.writeFile()上载图像会显示损坏的图像
我在将图像上传到Meteor的/public文件夹时遇到问题。流程运作完美无瑕,唯一的问题是图像被破坏了 X.htmlJavascript 使用fs.writeFile()上载图像会显示损坏的图像,javascript,node.js,meteor,ecmascript-6,fs,Javascript,Node.js,Meteor,Ecmascript 6,Fs,我在将图像上传到Meteor的/public文件夹时遇到问题。流程运作完美无瑕,唯一的问题是图像被破坏了 X.html <form class="documentForm" enctype="multipart/form-data"> <label for="signature">Upload image of Signature</label> <input type="file" name="signature" id="signat
<form class="documentForm" enctype="multipart/form-data">
<label for="signature">Upload image of Signature</label>
<input type="file" name="signature" id="signature" required>
<label for="panCard">Upload image of Pan Card Only.</label>
<input type="file" name="panCard" id="panCard" required>
<button class="btn btn-primary" type="submit">Upload</button>
<button class="btn btn-warning" id="reset">Reset</button>
</form>
},
为什么我的形象是腐败的?我应该怎么做才能纠正我的错误?您不能(或不应该-您选择)将文件添加到/public文件夹,原因有很多
'submit .documentForm': function(event, template){
event.preventDefault();
console.log(event.target.signature.files[0]);
var signatureImage = event.target.signature.files[0];
var panCardImage = event.target.panCard.files[0];
Meteor.call('upload', signatureImage, panCardImage, function(error, response){
if(error){
Bert.alert("<strong>Error !</strong> Some Problem occured while submitting documents.", 'danger', 'fixed-top' );
} else if(response){
Bert.alert("<strong>Success !</strong> Documents uploaded.", 'success', 'fixed-top' );
}
});
return false;
}
'upload'(signatureImage, panCardImage){
const fs = Npm.require('fs');
var signatureFileName = Meteor.userId() + "_signature.jpg";
var panCardFileName = Meteor.userId() + "_pancard.jpg";
var path = process.env['METEOR_SHELL_DIR'] + '/../../../public/img/userdocuments/';
/*var encoding = {encoding: 'binary'};*/
fs.writeFile(path + signatureFileName, signatureImage, Meteor.bindEnvironment(function (err) {
if (err) {
log.error(err);
} else {
log.debug("Signature upload - " + Meteor.userId());
}
}));
fs.writeFile(path + panCardFileName, panCardImage, Meteor.bindEnvironment(function (err) {
if (err) {
log.error(err);
} else {
log.debug("Pan Card upload - " + Meteor.userId());
}
}));
return true;