我目前正在做一个Sails.js项目,我想将一个文件上传到服务器,并将指向该文件的链接保存在我的数据库中。
例如,当我单击add FILE时,它应该允许我从我的计算机中选择一个文件,当我单击submit时,它应该将该文件上载到我所指向的URL (如果不存在该文件夹,则创建该文件夹)。
这能用Sails.js来完成吗?如果是,怎么做?
谢谢!
发布于 2013-08-22 19:13:31
这将使你到达那里的大部分方式:https://github.com/balderdashy/sails/issues/27
您可以使用https://github.com/aconbere/node-file-utils创建目录等。
npm install file需要模块中的文件,其代码与./sails/issue/27中的代码相似。
发布于 2014-12-02 02:45:13
这个博客有一个处理文件上传的好例子--它对我有用:
http://maangalabs.com/blog/2014/08/12/uploading-a-file-in-sails/
upload: function (req, res) {
// Call to /upload via GET is error
if(req.method === 'GET')
return res.json({'status':'GET not allowed'});
var uploadFile = req.file('uploadFile');
console.log(uploadFile);
uploadFile.upload(function onUploadComplete(err, files) {
// Files will be uploaded to .tmp/uploads
// IF ERROR Return and send 500 error with error
if (err) return res.serverError(err);
console.log(files);
res.json({status:200,file:files});
});
}https://stackoverflow.com/questions/18379876
复制相似问题