3

This is my template file:

<form method="post" enctype="multipart/form-data"
    action="rest/fileUpload/save">

    <div class="col-md-5">
        <input type="file" class="browseFile" name="file">
    </div>
    <div class="col-md-4">
        <input type="submit" class="btn btn-success btn-md" 
               value="Upload" ng-click="uploadFile()" >
    </div>

</form>

How do I pass the file in the uploadFile Function?

2
  • <form method="post" enctype="multipart/form-data" action="rest/fileUpload/save"> <div class="col-md-5"> <input type="file" class="browseFile" name="file"> </div> <div class="col-md-4"> <input type="submit" class="btn btn-success btn-md" value="Upload" ng-click="uploadFile()" > </div> </form> Commented Oct 14, 2015 at 7:29
  • hope stackoverflow.com/questions/33119193/… will solve you problem! Commented Oct 14, 2015 at 8:03

2 Answers 2

3

Here is Sample upload app using angular js - http://www.tutorialspoint.com/angularjs/angularjs_upload_file.htm

Sign up to request clarification or add additional context in comments.

5 Comments

Its working for me! what is the error are you getting?
"xcel.html:57 Uncaught SyntaxError: Unexpected token :" "angular.js:38 Uncaught Error: [$injector:modulerr" i got this two error
i am simply copy the code and paste it in my html page. and i run it to my server. is it correct way
looks like something broken on tutorialspoint. Could you try updating service code as below - myApp.service('fileUpload', ['$https:', function ($https:) { replace all instances of ''$http:" with "$http"
do you have any sample?
-1

Here is the Complete Solution for uploading xls file to mongodb using mean


## In HTML ##
<input type="file" name="file" id="fileUpload" file-model="myFile" 
 title="ChooseYour file" onchange="return ValidateExtension()" 
 required="required" />
<button value="Upload" name="submit" class="btn btn-success" ng-
 click="UploadCompanyContact()"></button>

   ## In Controller ##
    $scope.UploadCompanyContact = function(){
var file = $scope.myFile;
alert(JSON.stringify(file.name));
    var uploadUrl = "/college/upload_company_contact";
    var fd = new FormData();
    fd.append('file', file);    
    $http.post(uploadUrl,fd, {

        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    })
    .success(function(response , status){
        if(response=="true"){
            $.gritter.add({
                text: 'Successfully company contacts  is uploaded',
                class_name: 'gritter-success'
            });
        } else {
            $.gritter.add({
                text: 'Oopss..!! Something went wrong',
                class_name: 'gritter-error'
            });
        }

            $scope.loadCollegeData();
    })
    .error(function(){
        $.gritter.add({
            text: 'Oopss..!! Something went wrong',
            class_name: 'gritter-error'
        });
    });

    ## In Route(nodejs) ##
   router.post('/college/upload_company_contact',function(req,res){
   var exceltojson; 
    uploadcsv(req,res,function(err){
        if(err){
                console.log("error"+err);

        }

        if(!req.file){
            console.log("error 1 ");
            return;
        }
         if(req.file.originalname.split('.')
    [req.file.originalname.split('.').length-1] === 'xlsx'){
            exceltojson = xlsxtojson;
        } else {
            exceltojson = xlstojson;
        }
          try {

            exceltojson({
                input: req.file.path,
                output: null, 
                lowerCaseHeaders:true
            }, function(err,result){
                if(err) {

                } 

                var data = result;
                console.log(JSON.stringify(data)+"asdfasdfasdfsdf");

         for(var i=0;i<data.length;i++){

        var newuploadCompanyContact = new uploadCompanyContact();
        newuploadCompanyContact._id= objectID();
        newuploadCompanyContact.college_id=req.user.reference_id;
        newuploadCompanyContact.company_name=data[i].company_name;
        newuploadCompanyContact.company_email=data[i].company_email;
        newuploadCompanyContact.company_contact=data[i].company_contact;
        newuploadCompanyContact.company_address=data[i].company_address;
        newuploadCompanyContact.company_city=data[i].company_city;
        newuploadCompanyContact.company_country=data[i].company_country;

      newuploadCompanyContact.contact_personname=data[i].contact_personname;

    newuploadCompanyContact.contact_personemail=data[i].contact_personemail;

  newuploadCompanyContact.contact_personmobile=data[i].contact_personmobile;


    newuploadCompanyContact.save(function(err,uploadcompany){
    if(err) {console.log("sorry data not inserted 
  :"+err);res.json("false");}
    if(uploadcompany){console.log("data inserted sucessfully : 
  "+uploadcompany);res.json("true");}
 });
  } 

            });
        } catch (e){
           console.log("catchhhhh");
        }

  })
 })

  ## In Model ##
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  var CollegeCompanySchema = new Schema({
_id: { type: String, unique: true, required: true },
college_id: {type: String},
company_name:{type:String,default:''},
company_email:{type:String,default:''},
company_contact:{type:String,default:''},
company_address:{type:String,default:''},
company_city:{type:String,default:''},
company_country:{type:String,default:''},
contact_personname:{type:String,default:''},
contact_personemail:{type:String,default:''},
contact_personmobile:{type:String,default:''},

});

 var collectionName = 'college-company';
 var CollegeCompany = module.exports = mongoose.model('CollegeCompany', 
  CollegeCompanySchema, collectionName);
  module.exports = CollegeCompany;

2 Comments

Please provide an explanation for the code, do not just dump it as an answer. Also, add any attributions / source links if necessary.
what explanation u need?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.