angularjsspring-bootng-file-upload

Multiple files upload with Spring Boot


I´m trying upload multiples images using ng-file-upload with Spring Boot. The problem is that angularjs send files in wrong order,no sequence, shuffled.

Select images 0 1 2 3 4 5 and when send need to be in that order. But Angularjs send the files in order 5 2 1 0 4 3.

why this happen? I wanna send images one by one in the order 0 1 2...35 36. Example of the problem.

Index.html

<!-- Input Save images-->
<input class="btn btn-primary" multiple type="button" 
upload-file="vm.fotos" ngf-select="vm.uploadPaginas($files,$invalidFiles)"
    id="file" accept="image/*" ngf-max-size="2MB" value="Selecionar fotos" />

uploadimages.js

function uploadPaginas(paginas, paginaErro) {
    vm.paginas = paginas;
    vm.paginaErro = paginaErro;
    if (paginas.length && vm.formPagina.$valid) {
        angular.forEach(paginas, function (pagina, count) {
            Upload.upload({
                url: '/api/pagina',
                method: 'POST',
                arrayKey: '',
                data: { paginas: pagina, nome: vm.pagina.descricao, capitulo: vm.pagina.capitulo, numCapitulo: count }
            }).then(function (data) {
                    console.log("File: "+pagina.name+"\n Count: "+count);
                })
        })
    } else {
        vm.mensagemPagina = "Não foi salvo";
    }
}

imagesController.java

/**
 * Save images
 * 
 * @param paginas
 * @param nome
 * @param Capitulo
 * @param numCapitulo
 * @return
 * @throws IOException
 */
@RequestMapping(value = "/pagina", method = RequestMethod.POST, consumes = { "multipart/form-data" })
public @ResponseBody ResponseEntity<PaginasEntity> cadastrarPaginas(
        @RequestParam(value = "paginas",required = false) MultipartFile paginas, @RequestParam(value = "nome",required = false) String nome,
        @RequestParam(value = "capitulo",required = false) Long Capitulo,
        @RequestParam(value = "numCapitulo", required = false) int numCapitulo) throws IOException {

    if (!paginas.isEmpty()) {
        numCapitulo++;

        System.out.println("capitulo:"+numCapitulo);
        CapitulosEntity capitulo = new CapitulosEntity();
        capitulo.setId(Capitulo);           

        System.out.println("nome:"+paginas.getOriginalFilename());
        PaginasEntity pagina = new PaginasEntity();
        pagina.setFotos(paginas.getBytes());
        pagina.setNome(nome);
        pagina.setCapitulo(capitulo);
        pagina.setNumeroPagina(numCapitulo);
        paginaService.cadastrar(pagina);
    } else {
        throw new RuntimeException("Erro ao salvar Pagina");
    }

    return new ResponseEntity<>(HttpStatus.CREATED);
}

Upload one file at a time is a good practice? Or not?


Solution

  • When select multiple images in sequence to upload, angularjs don't sent image in ascending order, the back-end receive image in order unorganized like 5 3 1 4 2 0 and save in on database. I don´t want this.

    My solution is use query ORDER BY ASC to organize the images, then when return api GET /pagina/{id} all images will to be in ascending order.

    PaginasRepository.java

    @Repository
    public interface PaginasRepository extends CrudRepository<PaginasEntity, Long> {
    
        @Query("SELECT p FROM PaginasEntity as p WHERE p.capitulo =:id ORDER BY numeroPagina ASC")
        public Page<PaginasEntity> buscarPaginaPorCapituloId(@Param("id") CapitulosEntity id, Pageable pageable);
    }