2016-07-15 194 views
1
var express = require("express"), 
    app = express(), 
    formidable = require('formidable'), 
    util = require('util'), 
    fs = require('fs-extra'), 
    qt = require('quickthumb'); 

// Use quickthumb 
app.use(qt.static(__dirname + '/')); 

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:\n\n'); 
    res.end(util.inspect({fields: fields, files: files})); 
    }); 

    form.on('end', function(fields, files) { 
    /* Temporary location of our uploaded file */ 
    var temp_path = this.openedFiles[0].path; 
    /* The file name of the uploaded file */ 
    var file_name = this.openedFiles[0].name; 
    /* Location where we want to copy the uploaded file */ 
    var new_location = 'uploads/'; 

    fs.copy(temp_path, new_location + file_name, function(err) { 
     if (err) { 
     console.error(err); 
     } else { 
     console.log("success!") 
     } 
    }); 
    }); 
}); 

// Show the upload form 
app.get('/', function (req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html' }); 
    var form = '<form action="/upload" enctype="multipart/form-data" method="post">Add a title: <input name="title" type="text" /><br><br><input multiple="multiple" name="upload" type="file" /><br><br><input type="submit" value="Upload" /></form>'; 
    res.end(form); 
}); 
app.listen(8080); 

我是节点js和表达js的新手。我跟着这个guy's blog来上传和调整图像大小。以上是代码部分。上传功能正在工作。然而,在调整大小功能而言,如果我想调整上传的图片,我得到这个错误:通过express js上传并调整图像大小问题

events.js:85 
     throw er; // Unhandled 'error' event 
      ^
Error: spawn identify ENOENT 
    at exports._errnoException (util.js:746:11) 
    at Process._handle.onexit (child_process.js:1053:32) 
    at child_process.js:1144:20 
    at process._tickCallback (node.js:355:11) 

回答

0

你安装imagemagick? Quickthumb需要它的工作。

+0

运行npm后我imagemagick,我得到[email protected] imagemagick。我相信我做到了。 – OtakuFitness

+0

@OtakuFitness不,它没有。根据文档,您必须将imagemagic安装为util,例如Linux上的apt-get install imagemagick'。你得到的错误是'quickthumb'找不到'spawn'的程序。请记住,它只是'imagemagic'的一个包装,所以你必须自己安装'imagemagic'。 –

+0

您是否阅读过https://github.com/zivester/node-quickthumb文档(您使用该lib)?最可能的错误是'imagemagic'没有安装。只要确保你已经在PATH中安装了'imagemagic'。如果不是,请修复它。如果这不起作用,让我知道 –