2017-07-04 73 views
0

我在将图片上传到流星/公共文件夹时遇到了一个问题。该流程完美无瑕,只有图像是腐败的。使用fs.writeFile()上传图片显示损坏的图片

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; 

},

为什么我的形象是腐败?我应该怎样做才能纠正我的错误?

回答

1

你不能(或不应该 - 你选择)文件添加到/公共文件夹中,有许多原因......

  1. 在发展的流星将重新启动 - 这可能会导致腐败
  2. /public在运行时的位置与您的源位置不同。
  3. ,其中流星代码部署的文件系统很可能是生产系统
  4. 在移动平台上的只读你不容易接触到保存设备上的文件和空间有限

虽然技术上可以在文件系统上定义应用程序可以保存文件的位置,然后将该位置符号链接到/ public下,或者运行另一个express服务器来提供这些文件,这不是最好的做法。

您应该将您上传的文件存储在诸如AWS S3等服务上,或将它们存储在Mongo数据库中。有几个软件包可以帮助您实现这一点,从我的头顶ostrio:文件,vsivsi:文件收集和jalik:ufs