nodejs fs.writeFile 在 post 之后失败

nodejs fs.writeFile failing after post

我在 angular 一侧裁剪并加载了一个文件。感谢https://github.com/danialfarid/ng-file-upload

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

我可以在开发者中看到文件window。我的问题似乎在 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 并且它是空的。

vagrant@vagrant-ubuntu-trusty-64:/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 .

任何帮助都很棒...谢谢..

菲尔

我暂时用的就是这个。我遇到图像尺寸不小的问题。

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!")
            }
        });
    });
});