我目前正在我的應用程序中創建一個文件上傳系統。我的後端是Sails.js(10.4),它作爲我單獨前端(Angular)的API。Sails.js檢查上傳文件到MongoDB與船長(有效的文件,圖像大小等)之前的東西
我選擇將上傳到我的MongoDB實例的文件存儲起來,並使用sails'build in file upload module Skipper。我正在使用適配器隊長網格(https://github.com/willhuang85/skipper-gridfs)將文件上傳到mongo。
現在,上傳文件本身並不是問題:我在我的客戶端上使用dropzone.js,它將上傳的文件發送到/ api/v1/files/upload。這些文件將被上傳。
要做到這一點我使用我的FileController下面的代碼:
module.exports = {
upload: function(req, res) {
req.file('uploadfile').upload({
// ...any other options here...
adapter: require('skipper-gridfs'),
uri: 'mongodb://localhost:27017/db_name.files'
}, function(err, files) {
if (err) {
return res.serverError(err);
}
console.log('', files);
return res.json({
message: files.length + ' file(s) uploaded successfully!',
files: files
});
});
}
};
現在的問題:我想要做的東西與他們的文件要上載之前。特別是兩件事:
- 檢查是否允許該文件:內容類型標題是否匹配我想允許的文件類型? (JPEG,PNG,PDF等 - 只是基本文件)。
- 如果文件是圖像,請使用imagemagick(或類似的東西)將其調整爲幾個預定義的大小。
- 添加也將保存到數據庫中的特定於文件的信息:對上傳文件的用戶的引用,以及該文件所屬模型(即文章/註釋)的引用。
我不知道從哪裏開始或如何實現這種功能。所以任何幫助將不勝感激!
因此,如果我正確理解這一點,我應該首先使用默認的.upload函數,它將上傳的文件存儲在.tmp/uploads目錄中,並在第一個上傳函數的回調中執行我的自定義東西(如檢查文件類型等),然後使用skipper-gridfs發送給Mongo?我仍然可以在第一個回調中的文件上調用.upload? – Darkstra 2014-08-28 11:21:26
我這麼認爲,是的。我沒有在任何地方看到「validate()」回調。 – 2014-08-28 11:27:59
也許我在做一些愚蠢的事情,但是當我在回調中嘗試以下內容時: files [0] .upload({'adapter:require('skipper-gridfs'),uri:'mongodb:// localhost: 27017/evolution_api_v1.files'},函數(err,files){...}); 控制檯將死掉吐出'對象沒有方法上傳'。有點像我的預期。此外,我覺得有必要兩次上傳文件,這很奇怪。在文件上傳之前攔截文件會更好。不知道是否可以通過:)。 – Darkstra 2014-08-28 12:20:52