在AppFog上使用ExpressJS上传文件

时间:2013-06-03 22:22:35

标签: node.js express

我在AppFog上更新了我的应用程序,我的应用程序可以上传不同文件夹中的文件,它可以在本地工作,但它在AppFog上不起作用...

var file_tmp = file.path;
var file_name = file.name;
var file_type = file.type;
var file = './public/files/'+folder+'/'+file_name;
fs.rename(file_tmp, file, function(err){
    if( err ) console.log(err);
});

我没有收到错误,但是我无法在浏览器中看到该文件,我可能犯了一个错误,我不确切地知道路径是否正确。

编辑#1

new.jade(创作)

  form.admin(action='/creations/save', method='post', enctype='multipart/form-data')
    div
      label(for='title') Title
      input(type='text', name='title', placeholder='Title...')
    div
      label(for='thumbnail') Thumbnail
      input(type='file', name='thumbnail')
    div
      label(for='moodboard') Moodboard
      input(type='file', name='moodboard')
    div
      label(for='description') Description
      textarea(name='content', name='description', cols='30', rows='5', placeholder='Description')
    div
      label(for='location') Location
      input(type='text', name='location', placeholder='Location...')
    div
      label(for='date') Date
      input(type='date', name='date')
    input(type='submit', value='Save')
    input(type='reset', value='Cancel')

app.js

app.post('/creations/save', restrictedArea, creations.save);

creations_controller.js

exports.save = function(req, res) {
    saveFile(req.files.thumbnail, 'thumbnails');
    saveFile(req.files.moodboard, 'moodboards');
    global.db.Creation.create({
        title: req.body.title,
        description: req.body.description,
        location: req.body.location,
        date: req.body.date,
        thumbnail: req.files.thumbnail.name,
        moodboard: req.files.moodboard.name
    }).success( function(creation){
        res.redirect('/glovebox');
    });
}

creations_controller.js中的saveFile函数:

var saveFile = function(file, folder){
    var file_tmp = file.path;
    var file_name = file.name;
    var file_type = file.type;
    var file = './public/files/'+folder+'/'+file_name;
    fs.rename(file_tmp, file, function(err){
        if( err ) console.log(err);
    });
}

0 个答案:

没有答案