2013-08-22 39 views
2

我目前正在開發一個Sails.js項目,我想將文件上傳到服務器,並將鏈接保存到我的數據庫中。Sails.js - 文件上傳

例如,當我點擊添加文件時,它應該允許我從我的計算機中選擇一個文件,當我點擊提交時,它應該將文件上傳到我指向的URL(如果文件夾不存在)。

這可以用Sails.js來完成嗎?如果是,如何?

謝謝!

回答

2

此博客具有處理文件上傳一個很好的例子 - 它的工作對我來說:

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}); 
    }); 
} 
+0

馬特,我得到:[]上線的console.log(文件) ;我錯過了什麼? – Suisse