我正在前端(angularjs)上传一个excel文件,并希望在后端接收它(grails资源文件)。我不应该在我的项目中使用GSP。所以如何接收通过发送的文件angularjs?
AngularJs代码,
$scope.uploadExcelFile =function (file) {
var doc = file
var filesArr = [];
if (doc) {
filesArr.push({file: doc});
uploadCurry(filesArr);
}
}
function uploadCurry(queue){
if (arguments.length) {
this.q = queue
}
if (this.q.length) {
var curr = this.q.pop();
upload(curr.file);
}
}
function upload(file) {
if (file) {
var postdata = {
file: file,
}
roleProfileService.importExcleFile(postdata).then(function(res) {
console.log(res);
})
}
}
Angular Service文件,
var importExcleFile = function(file,callback){
return $upload.upload({
url: '/api/importFile/profile',
method: 'POST',
file: file
}).progress(function(evt) {
_log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
roleProfileService.progress = 'Loading '+ evt.config.file.name +" : "+ parseInt(100.0 * evt.loaded / evt.total)+"%"
}).success(function(data, status, headers, config) {
_log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
roleProfileService.uploadsuccess = true;
if(callback) callback();
}).error(function(a,b,c,d,e){
_log("err", a,b,c,d,e);
roleProfileService.uploadfail = true;
});
}
Grails资源文件,
@Path('/api/importFile')
@Consumes(['application/json','text/plain','application/vnd.ms-excel'])
@Produces(['application/json'])
class KsaResource {
KsaService ksaService
@GET
@Path('profile')
Response getAllKsaData(MultipartFile file) {
//how to receive uploaded file here?
List<Job> jobSavedList = ksaService.importExcelData(file);
//rest of code
}
}
答案 0 :(得分:0)
我们可以编写一个控制器来接收文件
class ImportRoleProfileController {
SpringSecurityService springSecurityService;
def importUpload() {
User currentUser = springSecurityService.getCurrentUser();
def file = request.getFile('file')
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
int sheet_Num = workbook.getNumberOfSheets();