有错误:ENOENT在服务器上进行简单的图像上传

时间:2013-12-03 10:28:44

标签: node.js express

网址 ::

http://<----url---->/Details/?key=image

image.jpg文件,是 POST request

的结果

我的快递计划 ::

var express=require('express');
var mysql=require('mysql');
var fs=require('fs');
var http=require('http');

var app=express();

var connection=mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'root',
    database:'posting_only_image_DB'
});

connection.connect();

app.set('port',process.env.PORT||7002);
app.use(express.static(__dirname+'/public/images'));
app.use(express.bodyParser({uploadDir:'./uploads'}));

app.post('/Details/',function(req,res){
    var temp_path=req.files.key.path 
    var target_path='./public/images' + req.files.key.path;

    var path=req.files.key.name;

    connection.query('INSERT INTO MyTable(image)',[path],function(err,rows,fields)
    {
        console.log('Connection result error', +err);
    });

    //Now move the file from temporary location to permanent location
    fs.rename(temp_path,target_path,function(err){
        if(err) throw err;

    //Delete the temporary file
    fs.unlink(temp_path,function(err){
        if(err) throw err;
    });
    });

});

http.createServer(app).listen(app.get('port'),function(){
    console.log('Express server listening on port'+app.get('port'));
});

我正面临的错误 ::

Error: ENOENT, rename 'uploads/4542-1jk7wvf.jpg'

注意 ::我的uploads文件夹位于我的app.js应用运行的位置

任何输入都会有所帮助

{编辑}

{ key: 
   { originalFilename: 'images.jpg',
     path: '/tmp/4645-17577l8.jpg',
     headers: 
      { 'content-disposition': 'form-data; name="key"; filename="images.jpg"',
        'content-type': 'image/jpeg' },
     ws: 
      { _writableState: [Object],
        writable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        path: '/tmp/4645-17577l8.jpg',

我编辑的代码 ::

var express=require('express');
var mysql=require('mysql');
var fs=require('fs');
var http=require('http');
var crypto=require('crypto');

var app=express();

var connection=mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'root',
    database:'posting_only_image_DB'
});

connection.connect();

app.set('port',process.env.PORT||7002);

app.use(express.bodyParser());

app.post('/Details/',function(req,res,next){

    var file_name=req.files.key.originalFilename;
    console.log(file_name);

    crypto.randomBytes(8, function(ex, buf) {

        var array     = req.files.key.originalFilename.split('.');
        var type      = array[array.length - 1];
        var name      = buf.toString('hex') + '.' + type;

        fs.rename(req.files.key.path, './public/images/' + name, function(e) {
            next(e, name);
        });
    });
});

http.createServer(app).listen(app.get('port'),function(){
    console.log('Express server listening on port'+app.get('port'));
});

2 个答案:

答案 0 :(得分:1)

我之前在Mac上遇到过这个问题而且它与权限有关。改变

app.use(express.bodyParser({uploadDir:'./uploads'}));

app.use(express.bodyParser());

然后执行此操作以保存文件。

crypto.randomBytes(8, function(ex, buf) {

  var array     = req.files.key.name.split('.');
  var type      = array[array.length - 1];
  var name      = buf.toString('hex') + '.' + type;

  fs.rename(req.files.key.path, './public/uploads/' + name, function(e) {
    next(e, name);
  });
});

您可以获得唯一文件名的附加好处。别忘了要求('crypto')

修改

从表单发布时

<input type="file" name="key">

名称的名称是使用req.files时需要使用的名称。&lt;输入的名称&gt;

修改2

要保持相同的文件名,你要做的就是(你可以摆脱加密功能)

fs.rename(req.files.key.path, './public/uploads' + req.files.key. originalFilename, function(e) {
    // Do what ever else you need to do.
})

答案 1 :(得分:0)

检查目的地路径是否存在。您收到此错误错误:ENOENT,即使目标不存在或具有足够的权限,也要重命名source_path。