2017-07-04 76 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