Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 无限循环上传文件multer/express和文件';s尺寸=0千欧_Angularjs_Node.js - Fatal编程技术网

Angularjs 无限循环上传文件multer/express和文件';s尺寸=0千欧

Angularjs 无限循环上传文件multer/express和文件';s尺寸=0千欧,angularjs,node.js,Angularjs,Node.js,我正在尝试将excel文件上传到web应用程序上。 但当我上传文件时,web应用程序上有一个inifnite循环,但文件已正确上传,下面是代码: 更新 节点: var storage = multer.diskStorage({ destination: function(req,file,cb) { cb(null,'/uploads/') }, filename: function(req,file,cb) { cb(null, file.originalname

我正在尝试将excel文件上传到web应用程序上。 但当我上传文件时,web应用程序上有一个inifnite循环,但文件已正确上传,下面是代码:

更新

节点:

var storage = multer.diskStorage({
  destination: function(req,file,cb) {
    cb(null,'/uploads/')
  },
  filename: function(req,file,cb) {
    cb(null, file.originalname);
  }
});

var upload = multer({ storage : storage });

app.post('/upload', upload.single('file'), function(req,res) {
  var tmp_path = req.file.path;
  var target_path = './uploads/'+req.file.originalname;
  var source = fs.createReadStream(tmp_path);
  var dest = fs.createWriteStream(target_path);
  var stream = source.pipe(dest);
  stream.on('finish', function() {
     res.send();
    });
});
角度:

$scope.uploadFile = function() {
  var file = $scope.myFile;
  var uploadUrl = "/upload";
  var fd = new FormData();
  fd.append('file',file);
}

$http.post(uploadUrl,fd,{
     headers: {'Content-Type' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
});

使用Pierre Emmanuel Lallemant提出的解决方案,不再存在无限循环,但上传后文件已损坏,其大小为0 Ko,而不是850 Ko。要检测文件是否保存,以下代码应起作用:

dest.on('finish', function() {  
  res.send();
});
这一条必须有效:

source.pipe(dest).on('finish', function() {  
  res.send();
});

我正在使用multer,所以我没有编写所有流程的代码

我只想做:

app.post('/upload', upload.single('propt'), function (req, res) {
    res.send();
});

您必须在“/upload”回调中发送响应。保存文件后,调用res.send()。如果您不这样做,expressjs认为您的
/upload
调用尚未完成其工作,因此应用程序被阻止。@PierreManuellallemant如何检测文件是否保存?文件已上载,但其大小为0千而不是850千。您的解决方案不起作用,文件在上载后已损坏,其大小为0千而不是850千