无法在Heroku服务器上上传图像 [英] Can't upload images on Heroku server

查看:103
本文介绍了无法在Heroku服务器上上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传图片到Heroku服务器,在本地主机上的一切工作很好的问题。但在Heroku上传图片时,看起来一切顺利,但在刷新页面之后,我收到了404(Not Found)的错误消息,因此看起来没有上传。



这里是我的代码

$ $ p $ $ $ $ ),
server = app.listen(process.env.PORT || 5000),
fs = require('fs-extra'),
im = require('imagemagick'),
util = require('util'),
formidable = require('formidable');
$ b app.post('/ upload',function(req,res){
var form = new formidable.IncomingForm();


form.parse(req,function(err,fields,files){
res.writeHead(200,{'content-type':'text / plain'});
res。 write('received upload:\\\
\\\
');
console.log(fields +''+ files);
res.end(util.inspect({fields:fields,files:文件}));
));

form.on('end',function(fields,files){
var that = this;
//临时存储镜像
var temp_path = that.openedFiles [0] .path;
//上传镜像名称
var file_name = that.openedFiles [0] .name;
var dirpath = __dirname +'/ app';
var opt = {
thumb:{
width:150,$ b $ height:150,
dest:'/ uploads /缩略图/'
},
原始:{
dest:'/ uploads / original /'
}
};

//获取图片的扩展名
var extension;
(function(type){
if(type =='image / jpeg'|| type =='image / jpg'){
extension ='.jpg';
)else if(type =='image / png'){
extension ='.png';
}
})(that.openedFiles [0] .type);
$ b fs.copy(temp_path,dirpath + opt.original.dest + id + extension,function(err){
if(err){
console.error(err) ;
} else {
//缩略图
im.crop({
srcPath:dirpath + opt.original.dest + id +扩展名,
dstPath:dirpath + opt.thumb.dest + id + extension,
width:opt.thumb.width,
height:opt.thumb.height,
质量:1,
gravity:'Center '
},函数(err,stdout,stderr){
if(err){console.log('Thumbnail is not generated')}
else {
}
});
}
});
});
});

哪里可能有问题?

解决方案

在云环境中,您绝不应该将图像上传到实例本身。总是使用一个像S3这样的中央存储来存储您上传的文件。



在云环境中开发时,请始终记住当前正在运行的框你的代码只能活一段时间(几分钟,几小时)。因此,如果实例出现故障,您将丢失所有上传的文件。



例如,如果您有2个实例,并且您的用户上传图像以安装x 。现在,当第二个用户尝试查看图像,但负载平衡器将请求发送到实例y时,图像未找到,因为该实例中不存在该图像。



为了调试你的脚本,我会添加更多的日志记录。然后在Heroku中,您可以使用:

  heroku日志-t -a appname 

观看应用程序的日志。

Got some problem with uploading images to heroku server, on localhost everything work great. But on heroku when I upload image, it looks that all goes well but after refreshing the page I got a error message 404 (Not Found) so it seems that it doesn't uploaded.

Here is my code

var express = require('express'),
    app = express(),
    server = app.listen(process.env.PORT || 5000),
    fs = require('fs-extra'),
    im = require('imagemagick'),
    util = require('util'),
    formidable = require('formidable');

app.post('/upload', function (req, res){
  var form = new formidable.IncomingForm();

  // show respond
  form.parse(req, function(err, fields, files) {
    res.writeHead(200, {'content-type': 'text/plain'});
    res.write('received upload:\n\n');
    console.log(fields + ' ' + files);
    res.end(util.inspect({fields: fields, files: files}));
  });

  form.on('end', function(fields, files) {
    var that = this;
    // temporary storage of image
    var temp_path = that.openedFiles[0].path;
    // uploaded image name
    var file_name = that.openedFiles[0].name;
    var dirpath = __dirname + '/app';
    var opt = {
      thumb : {
        width: 150,
        height: 150,
        dest: '/uploads/thumbnails/'
      },
      original : {
        dest : '/uploads/original/'
      }
    };

    // get extension of image
    var extension;
    (function(type) {
      if (type == 'image/jpeg' || type == 'image/jpg') {
        extension = '.jpg';
      } else if (type == 'image/png') {
        extension = '.png';
      }
    })(that.openedFiles[0].type);

    fs.copy(temp_path, dirpath + opt.original.dest + id + extension, function(err) {
      if (err) {
        console.error(err);
      } else {
        // thumbnails
        im.crop({
          srcPath: dirpath + opt.original.dest + id + extension,
          dstPath: dirpath + opt.thumb.dest + id + extension,
          width: opt.thumb.width,
          height: opt.thumb.height,
          quality: 1,
          gravity: 'Center'
        }, function (err, stdout, stderr){
          if (err) {console.log('Thumbnail is not generated')}
            else {
            }
        });
      }
    });
  });
});

Where could be a problem?

解决方案

In a cloud environment you should never upload images to the instance itself. Always use a central storage like for example S3 to store your uploaded files.

When you develop in a cloud environment you should always keep in mind that the current box you are running your code will only live for a certain amount of time (minutes, hours). So if the instances goes down you'll lose all your uploaded files.

Another problem can occur when you for example have 2 instances and your user uploads an image to instace x. Now when a second user tries to view the image but the load balancer will sent the request to instance y the image is not found because it doesn't exist on that instance.

For debugging your script I would add some more logging. Then in Heroku you could use :

heroku logs -t -a appname

to watch the logs of your application.

这篇关于无法在Heroku服务器上上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆