使用fs.writeFile()上传的图片显示损坏的图片 [英] Image Upload using fs.writeFile() shows corrupt Images
问题描述
将图片上传到Meteor的/ public文件夹时遇到问题。 Flow完美无瑕,只有图像损坏了。
I am facing a issue while uploading the image to Meteor's /public folder. The Flow works flawless, only the thing is the images are corrupt.
X.html
<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>
X.js
'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;
}
Meteor.method();
'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;
},
为什么我的图像是腐败?我该怎么办才能纠正我的错误?
why my image is corrupt? what should I do to rectify my mistake?
推荐答案
您不能(或不应该-选择)将文件添加到/ public文件夹,出于多种原因...
You can't (or shouldn't - you choose) add files to the /public folder, for a number of reasons...
- 正在开发的流星将重新启动-这可能导致损坏
- / public在运行时的位置与您的源位置不同。
- 部署流星代码的文件系统可能是在生产系统上只读
- 在移动平台上,您不容易访问将文件保存在设备上,而且空间有限
- In development meteor will restart - this may be causing the corruption
- The location of /public at run time is not the same as where your source is.
- The file system where the meteor code is deployed is likely to be read only on the production system
- On mobile platforms you don't have easy access to save files on the device, and the space is limited
虽然在技术上可以在文件系统上定义一个位置,您的应用可以在其中保存文件,然后将该位置符号链接到/ public下的某个位置,或者运行另一个快递服务器只是提供这些文件,但这并不是最佳实践。
While it is technically possible to define a location on the file system where your app can save files, and then either symlink this location in so that it's somewhere under /public, or run another express server just to serve up those files, it's not really best practice.
您应该选择将上传的文件存储在A之类的服务上WS S3,或将它们存储在Mongo数据库中。 ostrio:files,vsivsi:file-collection和jalik:ufs
You should either look to store your uploaded files on a service such as AWS S3, or store them in the Mongo database. There are several packages which can help you achieve this, off the top of my head ostrio:files, vsivsi:file-collection and jalik:ufs
这篇关于使用fs.writeFile()上传的图片显示损坏的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!