Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用fs.writeFile()上载图像会显示损坏的图像_Javascript_Node.js_Meteor_Ecmascript 6_Fs - Fatal编程技术网

Javascript 使用fs.writeFile()上载图像会显示损坏的图像

Javascript 使用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

我在将图像上传到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="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文件夹,原因有很多

  • 在开发中meteor将重新启动-这可能会导致损坏
  • 运行时/public的位置与源所在的位置不同
  • 部署meteor代码的文件系统可能在生产系统上是只读的
  • 在移动平台上,您不容易在设备上保存文件,而且空间有限
  • 虽然从技术上讲,可以在文件系统上定义一个应用程序可以保存文件的位置,然后将此位置符号链接到/public下,或者运行另一台express服务器来提供这些文件,但这并不是最佳做法

    您应该将上传的文件存储在AWS S3等服务上,或者将其存储在Mongo数据库中。有几个包可以帮助您实现这一点,在我的脑海中,ostrio:files、vsivsi:filecollection和jalik:ufs

    '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;