上传文件SailsJS

时间:2018-09-17 16:59:45

标签: vue.js sails.js

我正在尝试从VueJS Web应用程序将文本文件上传到运行SailsJS的服务器。我可以确认VueJS正在发送文件,但是我一直在服务器上收到错误消息。我的代码如下:

uploadData(req, res) {
  Logger.info(req.file('file'));

  req.file('file').upload({
    dirname:'../../tmp',
    saveAs:"hello"
  },function onUploadComplete(err,files){
    if(err){
      console.log(err);
      return res.serverError(err);
    }
    if(!files.length) {
      return res.serverError(new Error('No file Uploaded'))
    }
    res.ok(files);
  });
},

我得到的错误如下:

2018-09-17T16:55:07.736Z] info:  info: Custom response `res.serverError()` called with an Error: Error: Request aborted
    at IncomingMessage.onReqAborted (/home/ubuntu/server/node_modules/multiparty/index.js:183:17)
    at IncomingMessage.emit (events.js:182:13)
    at IncomingMessage.EventEmitter.emit (domain.js:442:20)
    at abortIncoming (_http_server.js:444:9)
    at socketOnClose (_http_server.js:437:3)
    at Socket.emit (events.js:187:15)
    at Socket.EventEmitter.emit (domain.js:442:20)
    at TCP._handle.close (net.js:599:12)

我在做什么错?我按照以下说明进行操作:

Sails Js Read uploaded file content

https://sailsjs.com/documentation/reference/request-req/req-file

https://sailsjs.com/documentation/concepts/file-uploads

1 个答案:

答案 0 :(得分:0)

这有效:

uploadData(req, res) {
  Logger.info(req.file('file'));

  return req.file('file').upload({
    adapter: require('skipper-disk'),
    saveAs:"hello"
  },function onUploadComplete(err,files){
    if(err){
      console.error("ERROR");
      console.log(err);
      return res.serverError(err);
    }
    if(!files.length) {
      return res.serverError(new Error('No file Uploaded'))
    }
    res.ok(files);
  });
},