nodejs fs.writeFile在发布后失败

时间:2016-02-27 16:04:07

标签: javascript angularjs node.js ng-file-upload

我有一个文件被裁剪并加载在角边。感谢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);
  });
}

我可以在开发者窗口中看到该文件。我的问题似乎是在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 .

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

菲尔

1 个答案:

答案 0 :(得分:0)

这就是我暂时使用的内容。我有问题,图像的大小不小。

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