Upload file to node server with angular-file-uploa

2019-09-06 03:12发布

问题:

I'm sorry about my English. I am use MEAN stack for writting my app. I find out some modules for uploading image and angular-file-upload is my choice. But when I upload the image the percent show on console completed. I'm check upload directory. The file is uploaded but can not read in Image Viever.

Here my code on angular :

        $scope.onFileSelect = function($files) {    
            for (var i = 0; i < $files.length; i++) {
                var file = $files[i];
                $scope.upload = $upload.upload({
                    url: '/posts/upload/',
                    method: 'POST',                 
                    file: file,
                }).progress(function(evt) {
                    console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
                }).success(function(data, status, headers, config) {
                    // file is uploaded successfully
                    console.log(data);
                });

            }
        };

Here my code on Node JS :

         exports.upload = function(req, res) {      
                       var data = new Buffer('');
                          req.on('data', function(chunk) {
                          data = Buffer.concat([data, chunk]);
                       });
                       req.on('end', function() {
                              req.rawBody = data;     
                              fs.writeFile(config.root + path.sep + 'public/upload' +                    path.sep + uuid.v1(), data ,function(err){
                             if(err) throw err;
                              console.log('ok saved')

                       });
                       res.send('ok');

                    });
              }

I guess I do something wrong with Node but I can't find out it. Please tell me what my mistake.

回答1:

You need to send the preview url for the uploaded image back to angular.

On server side

 req.on('end', function() {
     req.rawBody = data;
     var imgUrl = '/upload' + path.sep + uuid.v1();

     fs.writeFile(config.root + path.sep + 'public' + imgUrl, data ,function(err){
         if(err) throw err;

         //send back the preview url
         res.jsonp({
             imgUrl: imgUrl
         })
 });

Client Side

 $scope.onFileSelect = function($files) {    
        for (var i = 0; i < $files.length; i++) {
            var file = $files[i];
            $scope.upload = $upload.upload({
                url: '/posts/upload/',
                method: 'POST',                 
                file: file,
            }).progress(function(evt) {
                console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
            }).success(function(data, status, headers, config) {
                // file is uploaded successfully
                console.log(data);
                $scope.imgurl = data.imgUrl;
            });

        }
    };

You can display image like this

<img ng-src="{{imgurl}}" ng-show="imgurl" alt="preview for uploaded image" >