2

//this is my controller

function addFile(file){
        alert("inside add file");
        customerService.uploadCsv(file).then(function(response){
            if(response=="success"){
                alert("file uploaded");
            }else{
                alert("something went wrong");
            }
        },
        function(error){
            alet("error")

        });
    }

//this is my service

  this.uploadCsv=function(file){
        alert("inside upload csv service ");
        return $http.post('http://localhost:3005/customers/csv').then(function(response){
            deferred.resolve(response.data);
            return deferred.promise;
        },
        function(error){
            deferred.reject(error);
            return deferred.promise;
        });
    };//end of uploadCsv function

I have to send file in key value,key is 'upl' and value is file.I ma taking file from view(html)

Azhar Khan
  • 125
  • 1
  • 2
  • 9

2 Answers2

1

By formData, See Document

  this.uploadCsv = function(file) {
    alert("inside upload csv service ");
    
    var fd = new FormData();
    fd.append('upl', file);
    
    $http.post('http://localhost:3005/customers/csv', fd, {
        transformRequest: angular.identity,
        headers: {
          'Content-Type': undefined
        }
      })
      .then(function(response) {
          deferred.resolve(response.data);
          return deferred.promise;
        },
        function(error) {
          deferred.reject(error);
          return deferred.promise;
        });
  }; //
Yin Gang
  • 1,443
  • 1
  • 10
  • 15
1

I see that you tagged this post with Node.js. If you're using Node, there is a useful module which make the file upload easy as pie : Multer

Just add :

var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

With /uploads already created on the server (name it as you want).

Then if you're using Express for example, just call it with :

app.post('/profile', upload.single('fileName'), function (req, res, next) { // code here });

At the end don't forget to add the enctype to your form in the view as describe here (multipart/form-data for a file check form).

And it's done !

Aethyn
  • 695
  • 1
  • 4
  • 16