I have a file being cropped and loaded on the angular side. Thanks to 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);
});
}
I can see the file in the developer window. My issue seems to be on the Nodejs side.
My issue is on the nodejs side. I have the file name and the file is being copied to the tmp folder with a new name.
pofPhil3.jpg
image/png
/tmp/nYCf_p6kI5h4LfMQffNHbRu1.jpg
POST /register/user/uploads 200 8.775 ms - 23
Hello World file.name pofPhil3.jpg
I have been trying to get the file and copy it to another folder.
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});
});
The file is being created and named in the new folder. The problem is the file is not a jpg and its empty.
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 .
any help is great...thanks..
Phil
This is what I am using for the time being. I am having issues with size of the image not being smaller.
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!")
}
});
});
});