In your HTML file create a file input and button
<input name="fileupload" type="file" file-model="fileupload">
<button ng-click="uploadFile()" class="btn btn-primary">Upload File</button>
In your script file controller function:
myApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
return $http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
}
}]);
myApp.controller('fileController',function($scope,$http,fileUpload){
$scope.uploadFile = function(){
var file = $scope.fileupload;
var uploadUrl = "../uploadFile";
fileUpload.uploadFileToUrl(file, uploadUrl)
.then(function(response) {
console.log(response) ;
$scope.fileData= response.data;
})
};
)};
IN java controller
@RequestMapping(value="uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFile(@RequestParam(value="file", required=true) MultipartFile file,HttpServletRequest request) throws IOException, ParseException {
String fileName=file.getOriginalFilename();
final ServletContext servletContext = request.getSession().getServletContext();
final File tempDirectory = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
final String saveDirectory = tempDirectory.getAbsolutePath();
int i = 1;
// Creates a workbook object from the uploaded excelfile
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
// Creates a worksheet object representing the first sheet
XSSFSheet worksheet = workbook.getSheetAt(0);
// Reads the data in excel file until last row is encountered
while (i <= worksheet.getLastRowNum()) {
// Creates an object for the UserInfo Model
// Creates an object representing a single row in excel
XSSFRow row = worksheet.getRow(i++);
System.out.println("File: " + row.getCell(7).getDateCellValue() + row.getCell(8).getDateCellValue()
+ " ----!");
String dateStringInUse = row.getCell(7).getDateCellValue().toString();
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date dateInUse = parseFormat.parse(dateStringInUse);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String useDate = format.format(dateInUse);
//assign value to a list model
list.add(Model);
}
String jsonStr=null;
Gson gson = new Gson();
jsonStr = gson.toJson(list);
System.out.println(jsonStr);
return jsonStr;
}
<input name="fileupload" type="file" file-model="fileupload">
<button ng-click="uploadFile()" class="btn btn-primary">Upload File</button>
In your script file controller function:
myApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
return $http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
}
}]);
myApp.controller('fileController',function($scope,$http,fileUpload){
$scope.uploadFile = function(){
var file = $scope.fileupload;
var uploadUrl = "../uploadFile";
fileUpload.uploadFileToUrl(file, uploadUrl)
.then(function(response) {
console.log(response) ;
$scope.fileData= response.data;
})
};
)};
IN java controller
@RequestMapping(value="uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFile(@RequestParam(value="file", required=true) MultipartFile file,HttpServletRequest request) throws IOException, ParseException {
String fileName=file.getOriginalFilename();
final ServletContext servletContext = request.getSession().getServletContext();
final File tempDirectory = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
final String saveDirectory = tempDirectory.getAbsolutePath();
int i = 1;
// Creates a workbook object from the uploaded excelfile
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
// Creates a worksheet object representing the first sheet
XSSFSheet worksheet = workbook.getSheetAt(0);
// Reads the data in excel file until last row is encountered
while (i <= worksheet.getLastRowNum()) {
// Creates an object for the UserInfo Model
// Creates an object representing a single row in excel
XSSFRow row = worksheet.getRow(i++);
System.out.println("File: " + row.getCell(7).getDateCellValue() + row.getCell(8).getDateCellValue()
+ " ----!");
String dateStringInUse = row.getCell(7).getDateCellValue().toString();
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date dateInUse = parseFormat.parse(dateStringInUse);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String useDate = format.format(dateInUse);
//assign value to a list model
list.add(Model);
}
String jsonStr=null;
Gson gson = new Gson();
jsonStr = gson.toJson(list);
System.out.println(jsonStr);
return jsonStr;
}