如何在角度js上传excel文件?

时间:2015-10-14 07:29:05

标签: angularjs excel

这是我的模板文件:

<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>

如何在uploadFile函数中传递文件?

2 个答案:

答案 0 :(得分:3)

以下是使用角度js的示例上传应用 - http://www.tutorialspoint.com/angularjs/angularjs_upload_file.htm

答案 1 :(得分:0)

以下是使用mean

将xls文件上传到mongodb的完整解决方案
## 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;