上传图片适用于本地但不是在部署后使用nginx

时间:2017-07-01 10:18:06

标签: javascript meteor nginx

我有一个流星应用程序,我有一张个人资料图片,我想通过上传一个新的图片来改变。因此,我在文件夹中上传了一个图像,并将该URL保存在mongo数据库中。这在本地工作得非常好,但是当我使用nginx部署应用程序时,这不再适用了。

这是我在客户端的javascript代码:

Template.profile.events({
    'change #imgUploadButton' : function(event){
        var selectedFile = event.target.files[0];
        var reader = new FileReader();
        var imgtag = document.getElementById("imageTool");
        imgtag.title = selectedFile.name;
        reader.onload = function(event) {
            imgtag.src = event.target.result;
            Meteor.call('saveProfilePicture', selectedFile.name, reader.result);
        };
        reader.readAsDataURL(selectedFile);
    }
});

所以,我称之为流星法:

Meteor.methods({
  saveProfilePicture: function(fileName, fileData) {
    var fs = Npm.require('fs');
    var path = process.env.PWD + '/var/profilePictures/';

   base64Data  =   fileData.replace(/^data:image\/png;base64,/, "");
   base64Data  +=  base64Data.replace('+', ' ');
   binaryData  =   new Buffer(base64Data, 'base64').toString('binary');
   var pictureName = currentUserId + '_' + fileName;
   pictureName = pictureName.replace(/\s/g, '');

   fs.writeFile(path + pictureName, binaryData, "binary", Meteor.bindEnvironment(function (err) {
    if (err) {
            throw (new Meteor.Error(500, 'Failed to save file.', err));
          } else {
            insertionProfilePicture(fileName);
          }
     }));
   }
});

并且:

var insertionProfilePicture = function(fileName){
   var name = currentUserId + '_' + fileName;
   name = name.replace(/\s/g, '');
   Meteor.users.update({_id: currentUserId},{$set:{profilePicture: 
   "var/profilePictures/" + name}}); 
}

那么,你知道为什么它在本地工作但不是在用nginx部署之后呢?

编辑: 这可能是nginx的权限问题。 在文件/etc/nginx/site-enabled/myApp中,我有:

# pass all requests to Meteor
location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade; # allow websockets
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP

    # this setting allows the browser to cache the application in a way compatible with Meteor
    # on every applicaiton update the name of CSS and JS file is different, so they can be cache infinitely (here: 30 days)
    # the root path (/) MUST NOT be cached
    if ($uri != '/') {
        expires 30d;
    }
}
location /static/ {
    root /var/uploads;
}

所以,也许我需要用位置配置制作一些东西,但我试试:

location /profilePictures/ {
    root/var/profilePictures;
}

它不起作用。

0 个答案:

没有答案