2017-11-01 4 views
0
  1. app.jsファイルのか分からない:は、AWSへのNode.jsを使用してファイルをアップロードしようが、私は私のコードで間違っていた

    var aws = require('aws-sdk'), 
        http = require('http'), 
        bodyParser = require('body-parser'), 
        fs = require('fs'), 
        path = require('path'), 
        methodOverride = require("method-override"), 
        express = require('express'); 
    
    var app = new express(); 
    
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(bodyParser.urlencoded({extended: true})); 
    app.use(bodyParser.json()); 
    app.use(express.static(path.join(__dirname, 'public'))); 
    app.use(methodOverride("_method")); 
    
    var config_path = path.join(__dirname, 'auth.json'); 
    
    aws.config.loadFromPath(config_path); 
    var s3 = new aws.S3(); 
    
    app.get('/buckets', function(req, res){ 
        s3.listBuckets(function (err, data) { 
        res.json(data); 
        }); 
    
    }); 
    
    app.get('/upload', function (req, res) { 
        res.render('upload'); 
    }); 
    
    app.post('/upload', function (req, res) { 
        var s3request = { 
        Body: fs.readFileSync(req.files.theFile.path), 
        Bucket: 'bucket-*********', 
        Key: req.files.theFile.name 
        }; 
    
    s3.putObject(s3request, function (err, data) { 
        res.render('upload', {done: true}); 
        }); 
    }); 
    
    const PORT = process.env.PORT || 5000; 
    app.listen(PORT); 
    
    module.exports = app; 
    
  2. layout.jade:

    doctype html 
    html 
        head 
        title intra 
        link(rel='stylesheet', href='/css/bootstrap.min.css') 
        body 
        block content 
    
  3. upload.jade:

    extends layout 
    
    block content 
        div.container 
        if (done) 
         p Upload complete 
        h3 Upload File 
        form(enctype="multipart/form-data", method="post") 
        label Photo: 
         p 
          input(type="file", name="theFile") 
         input(type="submit", value="Submit") 
    

が実行されます。 TypeError:未定義のプロパティ 'theFile'を読み取ることができません。

答えて

0

私は確認のためのテストを実行しませんでしたが、コードを確認して、配列にアクセスしようとしています。

Key: req.files.theFile.name 

req.filesので、あなたがあるためにあなたのコードをリファクタリングすることができます配列です:

Key: req.files[0].name 

これは、あなただけの1つのファイルをアップロードしていると仮定しています。

参照用にこの例を使用できます。 http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

関連する問題