2016-02-27 105 views
1

我有一個文件被裁剪並加載到角度邊。感謝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

回答

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

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

+0

這是我發現的一個例子,有點作用。我怎麼會把它寫成快遞流?如果我沒有弄錯,4.0需要中間件......你可以舉個更好的例子。謝謝 – philipfwilson

+0

我在我的答案中使用了formidable.IncomingForm。在我的答案中看到我使用了它。如果有一個,請舉一個更好的示例..謝謝。 – philipfwilson