nodejs fs.writeFile post后失败

问题描述:

我有一个文件被裁剪并加载到角度边。感谢https://github.com/danialfarid/ng-file-uploadnodejs fs.writeFile post后失败

SignUp2ControllerTest -- $scope.upload --> 
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4Xmy9CbAk61Ue+GVmZWZl7XX32337db9Veov0tCEhJIyFsc2iwQOefWJmFI7AIIztYBxETNgOIiYM42 
Angularjs side. 

$scope.upload = function (dataUrl) { 

    console.log(' SignUp2ControllerTest -- $scope.upload --> ', dataUrl); 

    Upload.upload({ 
    url: 'http://test.dev:3000/signup/user/uploads', 
    data: { 
     file: Upload.dataUrltoBlob(dataUrl) 
    }, 
    }).then(function (response) { 
    $timeout(function() { 
     $scope.result = response.data; 
    }); 
    }, function (response) { 
    if (response.status > 0) $scope.errorMsg = response.status 
     + ': ' + response.data; 
    }, function (evt) { 
    $scope.progress = parseInt(100.0 * evt.loaded/evt.total); 
    }); 
} 

我可以在开发人员窗口中看到该文件。我的问题似乎在Nodejs方面。

我的问题是在nodejs端。我有文件名,文件正在用新名称复制到tmp文件夹。

pofPhil3.jpg 
image/png 
/tmp/nYCf_p6kI5h4LfMQffNHbRu1.jpg 
POST /register/user/uploads 200 8.775 ms - 23 
Hello World file.name pofPhil3.jpg 

我一直在试图获取该文件并将其复制到另一个文件夹。

var multiparty = require('connect-multiparty'); 
var multipartMiddleware = multiparty(); 


router.post('/user/uploads', multipartMiddleware, function(req, res) { 
    var file = req.files.file; 
    console.log(file.name); 
    console.log(file.type); 
    console.log(file.path); 

    var imageBuffer = decodeBase64Image(req.files.file); 

    fs.writeFile('/Users/testuser/test_hold_files/' + file.name, imageBuffer, function (err) { 
     if (err) return console.log(err); 
     console.log('Hello World file.name' , file.name); 
    }); 
    res.status(200).jsonp({file: file.name}); 
}); 

该文件正在创建并在新文件夹中命名。问题是文件不是jpg,而是空的。

[email protected]:/Users/testuser/test_hold_files$ ls -alt 
total 16 
drwxrwxrwx 2 root root 4096 Feb 27 06:26 . 
-rw-rw-r-- 1 vagrant vagrant 15 Feb 27 06:26 pofPhil3.jpg 
-rw-rw-r-- 1 vagrant vagrant 15 Feb 27 06:16 philipMallCar.jpg 
drwxr-xr-x 4 root root 4096 Feb 27 06:08 . 

任何帮助是巨大的...感谢..

菲尔

+0

标记您的问题与NG-文件上传,文件的NodeJS上传等 – danial

+0

我不认为'无功ImageBuffer的= decodeBase64Image(req.files.file);'是必要的,你应该能够保存该文件没有解码编码等。 – danial

+0

仍在处理这个问题。 – philipfwilson

这是我在用的暂时。我遇到的问题与图像的大小没有变小。

var formidable = require('formidable'), 
    util = require('util'), 
    fs_extra = require('fs-extra'); 
This is my post to accept images. 

router.post('/user/uploads', function (req, res){ 
    var form = new formidable.IncomingForm(); 
    form.parse(req, function(err, fields, files) { 
     res.writeHead(200, {'content-type': 'text/plain'}); 
     res.write('received upload:\n\n'); 
     res.end(util.inspect({fields: fields, files: files})); 
    }); 

    form.on('end', function(fields, files) { 
     /* Temporary location of our uploaded file */ 
     var temp_path = this.openedFiles[0].path; 
     /* The file name of the uploaded file */ 
     var file_name = this.openedFiles[0].name; 
     /* Location where we want to copy the uploaded file */ 
     var new_location = "/Users/testUser/test_hold_files/"; 

     fs_extra.copy(temp_path, new_location + file_name, function(err) { 
      if (err) { 
       console.error(err); 
      } else { 
       console.log("success!") 
      } 
     }); 
    }); 
}); 
+1

是否有理由解码文件,而不是将其作为流管道,或者是你正在努力? – cdbajorin

+0

这是我发现的一个例子,有点作用。我怎么会把它写成快递流?如果我没有弄错,4.0需要中间件......你可以举个更好的例子。谢谢 – philipfwilson

+0

我在我的答案中使用了formidable.IncomingForm。在我的答案中看到我使用了它。如果有一个,请举一个更好的示例..谢谢。 – philipfwilson