SoFunction
Updated on 2025-04-13

Method for uploading Excel file asynchronously in angularjs $http

1. The html code of the file upload box is as follows

<form  enctype="multipart/form-data">
 <button  type="button" ng-click="import_asset()">Upload file</button>
 <input  type="file" style="display: none;"/>
</form>

*Note: Set the enctype property value of form to: multipart/form-data

2: The js code is as follows:

$scope.import_asset = function () {
 $("#file_asset").click();
};
$("#file_asset").on("change", function(){
 var formData = new FormData();
 var file = ("file_asset").files[0];
 if(){
  var fileName = ((".") + 1);
  if(fileName =="xlsx" || fileName =="xls"){
   ('file', file);
   $http({
    method:"post",
    url: + "/so/assetmanage/upload",
    data:formData,
    headers : {
     'Content-Type' : undefined
    },
    transformRequest : 
   }).then(function (response) {
    if( == 200){
     alert("The file upload was successful!!!");
    }else{
     alert("File upload failed!!!");
    }
   });
  }else{
   alert("The file format is incorrect, please upload a file with the suffix name of .xlsx and .xls.");
   $("#file_asset").val("");
  }
 }
});