Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm building a file upload functionality with my angularjs app that would upload a file to my node api that will ftp to a cdn server. Right now I'm stuck with just getting hte file. I tried using multer but I'm not sure how to prevent the save to redirect to an ftp.

Anyway, this is my code withoout multer

 <input type="file" multiple  file-model="fileRepo"/>


myApp.directive('fileModel', ['$parse', function ($parse) {
        return {
           restrict: 'A',
           link: function(scope, element, attrs) {
              element.bind('change', function(){
                $parse(attrs.fileModel).assign(scope,element[0].files)
                scope.$apply();
              });
           }
        };
     }]);

///controller///

$scope.saveFile = function(){
        var fd=new FormData();
        angular.forEach($scope.fileRepo,function(file){
            fd.append('file',file);
        });

        $scope.newFile.files = fd;

        FileService.uploadFile($scope.newFile)
.....

/// fileservice ///

uploadFile: function(file){
           var deferred = $q.defer();

var uploadUrl = '/api/file/ftp/new';
                var requestFileUpload = {
                        method: 'POST',
                        url: uploadUrl,
                        data: file.files
                    }
                var requestFileUploadConfig = {
                    transformRequest: angular.identity,
                    headers: { 'Content-Type': undefined }
                }
                $http.post(uploadUrl, file.files, requestFileUploadConfig)
                    .then(function(){
                     })

/// node part ///

router.post('/ftp/new',  function(req, res) {
        console.log('file is ' + JSON.stringify(req.body));
    });
share|improve this question
    
which html parser are you using? busboy? – jack.the.ripper Jun 16 at 17:47
up vote 1 down vote accepted

You'll have to use an HTML parser you are not going to be able to catch the file just by reading the request.

I'd recommend use busboy and connect-busboy then you are going to be able to read your file, this a small example:

req.pipe(req.busboy);
req.busboy.on('file',function(fieldname, file, filename, encoding, contentType){
    // get data 
    file.on('data',function(data){

    }).on('end', function(){

    });
});


req.busboy.on('field',function(fieldname, val){
    req.body[fieldname] = val;
});

req.busboy.on('finish', function() {
    // save file here
});
share|improve this answer
    
i see. i thought i can just read the file without a parser. would you recommend multer? if not, does busboy have a way to go straight and ftp the file to a server? – gdubs Jun 16 at 18:17
    
I haven't use multer but check it out, my recommendation is based on my experience but you can use the one you like the most, I'm not sure about uploading directly to a ftp I haven't tried that yet, however a parse will do the trick to resolve the issue posted in your question. – jack.the.ripper Jun 16 at 18:25
    
you're right. btw, does busboy just automatically just save the file to a destination folder? if that's the case how do i grab it so i can ftp it? if you know. – gdubs Jun 16 at 20:09
    
you'll have to save it by yourself, basically, what you are getting is a buffer – jack.the.ripper Jun 16 at 20:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.