我试图按照教程http://thejackalofjavascript.com/uploading-files-made-fun/在sailsjs上执行bluimp jquery文件上传,但不知道它将如何在sailsjs上运行。我试图实现在将文件发布到上传器时不起作用的代码。
/**
* UploadController
*
* @description :: Server-side logic for managing uploads
* @help :: See http://links.sailsjs.org/docs/controllers
*/
var path = require('path');
var appDir = path.dirname(require.main.filename);
var options = {
tmpDir: 'C:/Users/ibrahim/Desktop/test/uploader/assets/images/tmp',
uploadDir: 'C:/Users/ibrahim/Desktop/test/uploader/assets/images/files',
uploadUrl: 'C:/Users/ibrahim/Desktop/test/uploader/uploaded/files/',
maxPostSize: 11000000000, // 11 GB
minFileSize: 1,
maxFileSize: 10000000000, // 10 GB
acceptFileTypes: /.+/i,
// Files not matched by this regular expression force a download dialog,
// to prevent executing any scripts in the context of the service domain:
inlineFileTypes: /\.(gif|jpe?g|png)/i,
imageTypes: /\.(gif|jpe?g|png)/i,
copyImgAsThumb : true, // required
imageVersions :{
maxWidth : 200,
maxHeight : 200
},
accessControl: {
allowOrigin: '*',
allowMethods: 'OPTIONS, HEAD, GET, POST, PUT, DELETE',
allowHeaders: 'Content-Type, Content-Range, Content-Disposition'
},
storage : {
type : 'local'
}
};
var uploader = require('blueimp-file-upload-expressjs')(options);
module.exports = {
index: function(req, res, next){
console.log(appDir);
uploader.get(req, res, function (obj) {
res.send(JSON.stringify(obj));
});
},
'new': function(req, res, next){
res.view();
},
create: function(req, res, next){
uploader.post(req, res, function (obj) {
res.send(JSON.stringify(obj));
});
}
};
// new.ejs
<h1>Test Drive Upload</h1>
<form action="/upload/create" method="post"
enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
在这方面的任何帮助将受到高度赞赏。
答案 0 :(得分:1)
Sails内置Skipper用于文件处理。
你可以这样做
req.file('[file field]').upload({
dirname: 'path to save file' //options
}, function(err, files)
{
//callback once files are saved
});
注意:如果要提交其他字段和文件,则必须确保其他字段始终位于文件之前,否则req.body将为空。
这将有效
<input type='text' name='text' value='text'/>
<input type='file' name='file'/>
但船长不会从这个
中读取'text'<input type='file' name='file'/>
<input type='text' name='text' value='text'/>