2017-02-15 5 views
0

我在將圖像從表單上傳到我的s3存儲桶時遇到了一些問題。在圖像的當前部分被上傳,因此,例如19KB,而不是完整272KB,如果我嘗試打開圖像從我的水桶內,它的分解將圖像上傳到s3存儲桶節點js

app.post('/admin/addClub', (req, res) => { 
    if (!req.user) { 
    res.redirect('/admin'); 
    return; 
} 

// Upload image to S3 
var s3Bucket = new AWS.S3({ params: {Bucket: process.env.AWS_BUCKET, Key: process.env.AWS_ACCESS_KEY_ID} }) 
var data = { Key: req.body.imageBanner, // file from form 
      Body: req.body.imageBanner, // Not sure here 
      ACL: "public-read", 
      ContentType: helper.getContentTypeByFile(req.body.imageBanner) 
      }; 
s3Bucket.putObject(data, function(err, data){ 
    if (err) 
    { console.log('Error uploading data: ', data); 
    res.redirect('/admin/main'); 
    } else { 
    console.log('succesfully uploaded the image!'); 
    res.redirect('/admin/main'); 
    } 
}); 

有人能指教一下,我需要通過爲Body關鍵?我想這一定是我的問題

感謝

+0

這是一個有點離題,但'res.redirect(」/admin/main');'可能在if之外。 – JonyD

回答

2

您需要快遞,文件上傳包,讓您的快遞收到文件上傳整合。

要安裝運行:npm install --save express-fileupload

然後,你需要通過req.files.imageBanner.data(假設你的文件上傳現場看起來像<input name="imageBanner" type="file" />)作爲Body參數。下面是它應該是什麼樣子:

var fileUpload = require('express-fileupload'); 

app.use(fileUpload()); 

app.post('/admin/addClub', (req, res) => { 
    if (!req.user) { 
    res.redirect('/admin'); 
    return; 
    } 

    // Upload image to S3 
    var s3Bucket = new AWS.S3({ params: {Bucket: process.env.AWS_BUCKET, Key: process.env.AWS_ACCESS_KEY_ID} }) 
    var data = { Key: req.body.imageBanner, // file from form 
       Body: req.files.imageBanner.data, 
       ACL: "public-read", 
       ContentType: helper.getContentTypeByFile(req.body.imageBanner) 
      }; 
    s3Bucket.putObject(data, function(err, data){ 
    if (err) 
    { console.log('Error uploading data: ', data); 
     res.redirect('/admin/main'); 
    } else { 
     console.log('succesfully uploaded the image!'); 
     res.redirect('/admin/main'); 
    } 
    }); 
}); 

你可以找到快遞,文件上傳包這裏的文檔:

https://www.npmjs.com/package/express-fileupload

+0

謝謝,我會看看包裹 – Richlewis

+0

當然,看看它,讓我知道它是如何工作的。 – ablopez

+0

所以我似乎越來越''無法讀取屬性'imageBanner'null,''imageBanner'是'' – Richlewis